Reputation: 21
//Function to store the reverse of original array
void ReverseName(char name[], char reverse[]) {
int i, j = 0;
for (i = 0; name[i] != '\0'; i++) {} //loop to find size of name
for (i; i >= 0; i--) { //loop to go backwards from end of name
reverse[j] = name[i]; //should get stored here
j++; //run through array and populate it
}
}
It seems like the code isn't copying properly and the output is coming out blank. When I put a 'cout' to check name[], it detects and prints it no problem in reverse, the problem is the copying into another array.
Upvotes: 2
Views: 42
Reputation: 6125
The problem is that you start the second loop with the index of the '\0'
in i
, so the reversed string will have '\0'
in the first character and thus seem empty.
Replace this:
for (i; i >= 0; i--)
with:
while (i--)
After the second loop finishes, terminate the string:
reverse[j] = '\0';
Upvotes: 1