I have the following code:
char[] str1 = new char[1000];
int k = 9, n = 0;
for (int j = k - 1; j >= 0; j--) {
str1[n] = str[j];
n++;
}
str1[n] = '\0';
But when executing I get an error here:
str1[n] = '\0';
what does it say:
Index was outside the bounds of the array
Why do I get that error, in the code before executing I don't get errors, but when I run the program I get an error there, I appreciate your comments and responses.
If you have a variable string you shouldn't define a char array of 1000, that's not correct. Even less so if later
k
you define the variable with the fixed value 9, when it really isn't.If you are going to do code that depends on the string input apply that same logic
The idea is that you take the dimensions of the original string to define the dimensions of the arrays that you are going to loop through.