北航计算机机试07字符串统计
从输入的字符串中,统计空格,回车,TAB出现的次数
#include <stdio.h>
#include <string.h>
int main(){
int space_count,enter_count,tab_count;
space_count = enter_count=tab_count=0;
char ch,buf[1024];//设置数组容量值为最大
////////////////////////////////////////
ch=getchar();
int i=0;
while(ch!='\\') //以"\"作为输入标志
{
buf[i]=ch;
ch=getchar();
i++;
}
buf[i]='\0';
//////////////////////////////////////////利用while代替for 先取值,判断,主体,递增
int length = strlen(buf);
////////////////////////////////
for(i=0;i<length;i++){
ch = buf[i];
if(ch == ' '){
space_count++;
}
else if(ch == '\n'){
enter_count++;
}
else if(ch == '\t'){
tab_count++;
}
}
/////////////////////////////////
printf("%d,%d,%d",space_count,enter_count,tab_count);
return 0;
}