Hello,
I am a newbee. I would like to reverse a string in C. I got 2 problems but i do not know why:
1. For a input string "striNg" i got the reverseText result "0gZirts". This is correct, but why i always receive the "0" at the beginning?
2. For a input string "striNg for fun" i got a total wrong result "0nuf rof gZy/\225d\367\377\377\277".
Can you please help me. Thank you!
I am a newbee. I would like to reverse a string in C. I got 2 problems but i do not know why:
1. For a input string "striNg" i got the reverseText result "0gZirts". This is correct, but why i always receive the "0" at the beginning?
2. For a input string "striNg for fun" i got a total wrong result "0nuf rof gZy/\225d\367\377\377\277".
Can you please help me. Thank you!
PHP:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *revChar(char *chr) {
if (*chr == 'N') {
// Replace N with Z
char *chr = "Z";
return chr;
}
return chr;
}
char *revstr(char *str) {
size_t length = strlen(str);
char dest[length];
char charAtIndex[length];
char *result;
for (int i = length - 1; i >= 0; i--) {
strncpy(charAtIndex, &str[i], 1);
result = strcat(dest, revChar(charAtIndex));
}
return result;
}
int main () {
char text[] = "striNg";
printf("Text: %s\n", text);
printf("reverseText: %s\n", revstr(text));
return 0;
}