Why is the garbage value being printed after displaying the array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why is the garbage value being printed after displaying the array

Why is the garbage value printed after printing the character array. I know the problem is somewhere is the char array declaration, but i cant finf and fix it. Furthermore The garbge values are not displayed in dev c++ or sololearn codeplayground. But you can see them if you paste and run the code in Visual Studio. Code: https://code.sololearn.com/c8a19a22A176

21st May 2021, 7:28 AM
Muhammad Abdullah
Muhammad Abdullah - avatar
1 Answer
+ 5
Line 32: it should be `length + 1` to accommodate the null character. All C-style strings end with a null character, which marks the end of the string. Then in the StringManipulator::operator() method, you are not adding the null character at the end of `temp.characterString`. This is the real reason for the garbage values. As I said, the null character marks the end of the string. If it is not found, the output function will go out of the bounds of the string and keep printing till it finds a null character. So you need to add this line just after the for-loop `charachterString[length - 1] = '\0';`
21st May 2021, 8:35 AM
XXX
XXX - avatar