文字列を反転する関数を作成しようとしています: 文字列入力が の"Hello World"
場合、関数は を返す必要があり"dlroW olleH"
ます。ただし、関数を実行すると、文字列は同じままです。
void reversestring(char* s) {
char tmp; //tmp storing the character for swaping
int length; //the length of the given string
int i; //loop counter
//reverse the string of even length
length = strlen(s);
if (length % 2 == 0) { //if the length of the string is even
for(i = 0; i < (int) (length / 2);i++) {
tmp = s[length - i];
s[length - i] = s[i];
s[i] = tmp;
}
}
//reverse the string of odd length
if (length % 2 == 1) { //if the length of the string is odd
for(i = 0; i < (int) ((length + 1) / 2);i++) {
tmp = s[length + 1];
s[length + 1] = s[i];
s[i] = tmp;
}
}
}