编写C语言程序统计输入字符串的个数、

如题所述

第一种方法:统计输入字符串的长度
#include<stdio.h>
#include<string.h>
int main()
{
char buff[100]={0};
printf("请输入一个字符串:\t");
scanf("%s",&buff);
printf("你输入字符串的个数为:\t%d\n ",strlen(buff));
}
第二种方法:遍历输入字符串,直到结束字符'\0'
#include<stdio.h>
void main()
{
char str[1000],*p;
int word=0;
printf("请输入一个字符串:\t");
gets(str);
for(p=str;*p!='\0';p++)
word++;
printf("输入字符串的长度为:\t%d\n",word);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-11
#include<stdio.h>
#include<string.h>
int main()
{
int num = 1;
char str[10000];
gets(str);
if(strtok(str," "))
while(strtok(NULL," "))
{
num++;
}
printf("%d\n",num);getchar();getchar();
return 0;
}