C语言中字符串问题~

来源:百度知道 编辑:UC知道 时间:2024/05/21 17:09:26
用数组输入字符不知道个数的时候怎么初始化呢 比如输入一字符串然后反序的输出怎么实现啊

不知道输入字符大小的时候,可以用这个strlen函数来判断输入字符的长度。我写了个例子,可以运行。你看看吧
#include "stdio.h"
#include "string.h"
#define M 100
int main()
{
int i,n;
char str[M];
printf("请输入字符:");
gets(str);
n=strlen(str);
for(i=1;i<=n;i++)
printf("%c",str[n-i]);
printf("\n");
return 0;
}

sizeof

不知道个数。。。?没办法噜,char的数组开大点吧。
输入了一个字符串以后,可以用strlen来获得它的长度。
反序的话可以用自带的strrev反转。
#include "stdlib.h"
#include "string.h"
#include "Stdio.h"
int main(){
char a[1000];
scanf("%s",a);
printf("它的长度是:%d\n",strlen(a));
strrev(a);
printf("反转它以后是:%s",a);

system("pause");
return 0;
};

system("pause");
return 0;
};