这是代码
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char a[20],rev[20];
printf("enter the string");
scanf("%s",a);
int len=strlen(a);
for(int i=0;i<len;i++)
{
rev[i]+=a[len-i-1];
}
printf("%d \t \n string is \t %s",len,rev);
getch();
}
当我们给它一个没有空格的字符串时它可以正常工作:
input: welcome
len:7
output: emoclew
当我们给它一个带空格的字符串时:
input : welcome to this world
len:7
output:some other ascii chars that I have not seen so far. and the "len" is again 7 only
当我更改以下语句时:
scanf("%s",a) to gets(a);
我得到:
input :welcome to this world
len:21
output : something different. not the reverse of string...
在这种情况下,“len”是正确的,但输出是错误的。
到底发生了什么?上面的代码有什么问题?
最佳答案
关于c - 在 C 中查找字符串长度的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6482990/