解题思路:
解题时一定要看懂题目意思。写两个函数(一个是main函数,另一个是被调函数count),在main函数中输入字符gets(str),注意要将数组空间大小设置在100以上。我们在count函数中去统计从实参传过来的字符串中的各个字符的个数。利用一个for循环,取str的数组长度值strlen(str),并将其赋给n,作为循环的结束条件。




注意事项:
1.注意要调用#include<string.h>和#include<stdlib.h>

2.因为是字符数组,所以字符值要用单引号




参考代码:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

int count(char ch[100])

{

int i,n=strlen(ch);

int num=0,charc=0,blank=0,other=0;

for(i=0;i<n;i++)    //这里的i是小于n的,而不能等于n,否则会把数组的'\0'这个结束符作为其它符号而other多一

{

if(ch[i]>='0' && ch[i]<='9') num++;

else if(ch[i]>='a' && ch[i]<='z' || ch[i]>='A' && ch[i]<='Z') charc++;

else if(ch[i]==' ') blank++;

else other++;

}

printf("%d %d %d %d",charc,num,blank,other);

return 0;

}

int main()

{

char str[100];

gets(str);

count(str);

return 0;

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

程序的注释是解释性语句,您可以在 C++ 代码中包含注释,这将提高源代码的可读性。所有的编程语言都允许某种形式的注释。

C++ 支持单行注释和多行注释。注释中的所有字符会被 C++ 编译器忽略。
2020-06-03 19:07:25
  • «
  • 1
  • »