I'm writing this code to reverse the string, but somehow the last printf()
seems can not print out the update string. I want to know why.
#include <stdio.h>
int main() {
char x[] = "abcdefghijklmn";
int j = sizeof(x);
int i = 0;
char t = 'a';
printf("%s,%i\n", x, j / 2);
for (; i < j / 2; i++) {
t = x[i];
x[i] = x[j - i - 1];
x[j - i - 1] = t;
printf("%c,%i,%i\n", x[i], i, j - i - 1);
}
printf("%s\n", x);
return 0;
}
【LET ME SUMMARIZE】Thank you guys, after reading all your answers, I've rewrite the code, which is quite clear to show the difference between sizeof()
and strlen()
, also it was a simpler way to reverse your string. check it and leave your comment if you want.
#include <stdio.h>
#include <string.h>
void StringReverse(char *s) {
int i = 0;
int j = strlen(s) - 1;
printf("sizeof:%i,strlen:%i\n",sizeof(s),strlen(s));
for (; i < j + 1; i++)
printf("%c", s[j - i]);
}
int main() {
char x[] = "abcdefghijklmn";
printf("sizeof:%i,strlen:%i\n",sizeof(x),strlen(x));
StringReverse(x);
return 0;
}
The result is :
sizeof:15,strlen:14
sizeof:4,strlen:14
nmlkjihgfedcba
Then check this:
#include <stdio.h>
#include <string.h>
void StringReverse(char *s) {
int i = 0;
int j = strlen(s) - 1;
printf("sizeof:%i,strlen:%i\n",sizeof(s),strlen(s));
for (; i < j + 1; i++)
printf("%c", s[j - i]);
}
int main() {
char x[100];
puts("Input a tring to reverse:");
fgets(x,100,stdin);
printf("sizeof:%i,strlen:%i\n",sizeof(x),strlen(x));
StringReverse(x);
return 0;
}
And the result is:
Input a tring to reverse:
abcdefghijklmn
sizeof:100,strlen:15
sizeof:4,strlen:15
nmlkjihgfedcba
It is clear now that strlen()
alway try to give the real length of the string, but why the first length is 14, the second input way it is 15?
sizeof()
is defined by the number you put in the char x[] ,always include EOS;
and pointer variable to a string, sizeof()
always return the pointer length instead of the string length.
【Thanks to @David C. Rankin】Your code reminds me that array variable is always used as a pointer to be passed to the function, so the original string is always not preserved no matter you use common function(first code below) or pointer to funtion(second code below). Correct?
first:
#include <stdio.h>
#include <string.h>
void StringReverse(char *s) {
int i = 0;
int j = strlen(s);
printf("sizeof:%i,strlen:%i\n",sizeof(s),strlen(s));
char t = 'a';
for (; i < j / 2; i++) {
t = s[i];
s[i] = s[j - i - 1];
s[j - i - 1] = t;
}
printf("%s\n", s);
}
int main() {
char x[]="abcdefghijklmn";
/* if you instead write char *x="abcdefghijklmn"; here
the compiler will encounter error, because a pointer to
a 【string literal】 can not be used to modify the string.
*/
printf("sizeof:%i,strlen:%i\n",sizeof(x),strlen(x));
StringReverse(x);
printf("\n%s\n", x); //original string x is not preserved.
return 0;
}
the result is:
sizeof:15,strlen:14
sizeof:4,strlen:14
nmlkjihgfedcba
nmlkjihgfedcba
second:
#include <stdio.h>
#include <string.h>
char *StringReverse(char *s) {
int i = 0;
int j = strlen(s);
printf("sizeof:%i,strlrn:%i\n", sizeof(s), strlen(s));
char t = 'a';
for (; i < j / 2; i++) {
t = s[i];
s[i] = s[j - i - 2];
s[j - i - 2] = t;
}
return s;
}
int main() {
char x[100];
puts("Input a tring to reverse:");
fgets(x, 100, stdin);
printf("sizeof:%i,strlrn:%i\n", sizeof(x), strlen(x));
StringReverse(x);
printf("\n%s\n", x); //original string x is not preserved.
return 0;
}
the result is:
Input a tring to reverse:
abcdefghijklmn
sizeof:100,strlrn:15
sizeof:4,strlrn:15
nmlkjihgfedcba
So if you want to preserve the original string, you have to use some other way:
...to be continue