调凯子 3星
共回答了362个问题采纳率:97.5% 评论
我推荐2种方法:-------------------------方法一:用库函数strlen()举例:#include<stdio.h>#include<string.h>int main(){ char a[100]; int num; printf("请输入字符串,enter键结束:"); gets(a); num=strlen(a);/*这里,strlen()就是计算a的输入长度,一般,有了这一步,我会加一步a[num]='\0';*/ printf("你刚才输入了%d个字符\n",num); return 0;}---------------------------------方法2:直接输入时计算,这种方法,纯粹是在一边输入一遍计算具体是,定义一个count,然后做判断,没输入一个字符,count加1,即count++;举例:#include<stdio.h>#include<string.h>int main(){ char a[100]; int num=1;//进入while循环的条件 int count=0;//count计算字符输入情况 int i=0;//数组下标 printf("请输入字符串,按阿拉伯数字0结束:"); while(num) { a[count]=getchar(); if(a[count]=='0') //当输入0,结束 num=0; count++; } printf("你刚才输入了%d个字符\n",count); return 0;}注意,第2种方法中,你按enter键,也算字符,只不过是空字符,你看不到而已。希望对你有帮助。
9小时前
猜你喜欢的问题
2天前1个回答
2天前1个回答
2天前1个回答
2天前2个回答
2天前1个回答
2天前2个回答
热门问题推荐
3个月前1个回答
3个月前2个回答
3个月前1个回答
3个月前2个回答
3个月前1个回答
2个月前1个回答
1个月前1个回答
3个月前4个回答
2个月前3个回答