首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

怎么修改这个计算字符个数的程序

2013-06-25 
如何修改这个计算字符个数的程序?#include stdio.hint main(){char s[5]int count0int i0printf(P

如何修改这个计算字符个数的程序?
#include <stdio.h>

int main(){
char s[5];
int count=0;
int i=0;

printf("Please input string:\n");

for(i=0;i<5;i++)
{
scanf("%c",&s[i]);
}


while(s[i]!='#')
{
i++;
count++;
}

printf("The string numbers is: %d\n",count);

return 0;

}
          程序要求输入一个字符串,设立一个计数器计算字符的个数,只要没遇到‘#'符号,就计数,但是我输入‘helloworld',运行之后,系统给出的数字是5197这个无规律的数。麻烦高手帮忙修改 C
[解决办法]


#include <stdio.h>

int main(){
char s[1024];
int count=0;
int i=0;

scanf("%s", s);

while(s[i]!='#' && s[i] != '\0')
{
i++;
count++;
}

printf("The string numbers is: %d\n",count);

return 0;

}

[解决办法]
#include<stdio.h>

main()
{
int c,i=0;

while((c=getchar())!=EOF)
{

if('#'==c)
{
break;
}
else if(' '==c)
;
else
{
++i;
}
}

printf("The strlen is %d\n",i);
}

[解决办法]
for(i=0;i<5;i++)
{
scanf("%c",&s[i]);
}


while(s[i]!='#')
{
i++;
count++;
}
以上两个loop之间,i是不是重新置0比较好?

[解决办法]
你的输入的字符中根本就没有“#”。。。。
可以改成这样
int iNum;
iNum=strstr(s, "#")-s > 0;
if(iNum>0)
{
    printf("%d\n", iNum);
}
else
    printf("%d\n", strlen(s));

热点排行