输入一个字符串,统计出其中空格的个数 C语言

作者&投稿:卓畏 (若有异议请与网页底部的电邮联系)
c语言编程 输入一个字符串,统计其中空格的个数~

char str[BUFSIE + 1] = {0};
int spaseCnt;

gets(str);
/*or
fgets(str,BUFSIZ, stdin);
str[ strchr(str,'
') - str ] = '\0';
*/
spaseCnt = 0;
size_t i = 0;
while (str[i] != '\0') {
 if (isspase(str[i])) {
  ++spaseCnt;
 }
 ++i;
}
printf("空格数量:%d", spaseCnt);

代码如下:
#include
int main(){
char stringss[1024];
int i,num[4]={0};
int j = 0;
char c;
while((c=getchar()) !='
'){
stringss[j] = c;
j++;
}
for(i=0;i<j;i++)//统计字符串,遇到'\0'结束
{
if(stringss[i]>='0'&&stringss[i]<='9')//统计数字个数
num[0]++;
else if(stringss[i]==' ')//统计空格
num[1]++;
else if(stringss[i]>='A'&&stringss[i]='a'&&stringss[i]<='z')//统计字符
num[2]++;
else
num[3]++;//其他
}
printf("出现的数字%d个,出现的空格%d个,出现的字母%d个,其他字符%d个
",num[0],num[1],num[2],num[3]);
}
程序运行结果如下:

扩展资料
C语言中对字符串的统计:可以通过循环数组的方式去一个一个的比较字符,然后进行统计。首先可以通过循环的方式读取每个数字,直到读到换行符“
”,就结束读取,把读取的字符存到数组中,同时记录下字符的长度,然后进行循环统计,打印出现个数就完成了。
参考资料:百度百科-C语言

思路:统计字符串中的空格,所以该字符串中有空格,则输入只能使用gets函数,再依次遍历该字符串,判断字符是否是空格,如果是,则空格个数自加1。

参考代码:

#include<string.h>
#include<stdio.h>
#include<math.h>
int main()
{
int sum=0,i;
char a[100];
gets(a);
for(i=0;a[i]!='\0';i++)
if(a[i]==' ')
sum++;
printf("%d
",sum);
return 0;
}
/*
输出: 
af  adf  asfd
4
*/


#include <stdio.h>
void main()
{
char c;
int space=0;
printf("输入一个字符串:\n");
while((c=getchar())!='\n')
{
if(c==' ')
space++;
}
printf("空格个数:%d\n",space);
getchar();
}

#include <stdio.h>
int main()
{
char s[1000];
int count = 0;
int i;
gets(s);
for(i=0; i<1000 && s[i]!='\0'; i++)
if(s[i] == ' ') count++;
printf("空格个数:%d\n", count);
system("pause");
return 0;
}

C语言字符串的学习,输入指定字符串,并且计算字符串的位数



#include <stdio.h>
#include <string.h>

int main()
{
char s[1000];
int len,c,i;
while(gets(s))
{
len = strlen(s);
c = 0;
for(i=0;i<len;i++)
{
if(s[i]==' ')
c++;
}
printf("Space: %d\n",c);
}
}


输入一个字符串存入字符数组,统计其中大写字母,小写字母,数字和其它字 ...
cout<<"输入字符串:";gets(str);while(str[i]!='\\0'){ if(str[i]>='0'&&str[i]<='9')n1++;else if(str[i]>='a'&&str[i]<='z')n2++;else if(str[i]>='A'&&str[i]<='Z')n3++;else n4++;i++;} cout<<"其中的数字个数是: "<<n1<<endl<<"其中的小写字母个数...

从键盘上输入一个字符串,统计字符串中的字符个数。不许使用求字符串长度...
include<stdio.h> void main(){ int i,n,m=0;char a[100];printf("请输入字符串:\\n");gets(a);for(i=0;a[i]!='\\0';i++)m++;printf("字符串的长度为:%d\\n",m);}

c语言:输入一个字符串,统计数字字符的个数
C语言字符串的学习,输入指定字符串,并且计算字符串的位数

(2)从键盘输入一个字符串和一个字符,要求统计字符串长度和该字符在字符...
你的问题是统计字符串长度和字符在字符串中出现率。字符串长度直接用string. h下的函数strlen来获取。而出现率,只要遍历字符串和字符比较,统计字符串中该字符的个数,再用出现个数除以字符串长度就可以了。

C语言:输入一个字符串,统计有多少个整数并输出
也就是十进制的48-57,在统计的时候用某个字符的ascii码值减去48就等到了这个字符对应的数值。digit=*(pstr+i-1)-48; 比如这一句中的*(pstr+i-1) 如果是49的话,就代表它是字符'1' 减去48恰好得到1,也就是得到了字符'1'所对应的数值1 ...

C++,输入字符串,统计各个字母出现次数,显示字母和次数,求大神知道咩
1、首先我们选择鼠标单击文件里的“新建”项目。2、选择为:控制台应用程序 - “名称:计算字符串中每个字母的出现次数 - ”确定。3、之后我们选择确定系统生成的代码。4、首先编写一个字符串进行测试。5、测试代码1:使用Dictionary集合和循环来确定测试代码。6、测试结果1:程序成功运行后显示每个字母的...

输入一个字符串,统计出其中空格的个数 C语言
思路:统计字符串中的空格,所以该字符串中有空格,则输入只能使用gets函数,再依次遍历该字符串,判断字符是否是空格,如果是,则空格个数自加1。参考代码:include<string.h>#include<stdio.h>#include<math.h>int main(){int sum=0,i;char a[100];gets(a);for(i=0;a[i]!='\\0';i++)...

JAVA编写:输入一个字符串然后统计某字符的个数输出
就是用一个for循环,遍历字符串,如果发现那个字符,个数就加一。int count = 0;\/\/个数 char c='a';String s = "abdelabdbals";for(int i=0;i<string.length();i++){ if(s.charAt(i)==c) count++;} System.out.println(count);...

从键盘输入一个字符串存入字符数组,统计数字,字母,空格和其他字符的个...
str[i]>='a' && str[i]<='z'))ch++;\/\/字母 else if(str[i]>='0' && str[i]<='9')num++;\/\/数字 else if(str[i]==' ')blank++;\/\/空格 else other++;i++;} printf("数字%d个,字母%d个,空格%d个,其他%d个\\n",num,ch,blank,other);return 0;} 望采纳,谢谢!!

用c语言设计一个程序:输入一串字符串,统计其中包括多少单词,单词之间...
include<stdio.h>int main(){int i,n=0; char s[300]; gets(s); for(i=0;s[i];) {while(s[i]&&s[i]==' ')i++; if(s[i])n++; while(s[i]&&s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]<='z')i++; } printf("%d",n); return 0;} ...

七台河市15117421804: c++输入一行字符串,要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数.谁会啊? -
斐残璇美: #include void main() { char sen[256]; int ben=0,men=0,spa=0,num=0,oth=0; int i; gets(sen); for(i=0;i<(int)strlen(sen);i++) { if(sen[i]>='a'&&sen[i]<='z') { ben++; } else if(sen[i]>='a'&&sen[i]<='z') { men++; } else if(sen[i]>=' ') { spa++; } else if(sen[i]>='0'&...

七台河市15117421804: 求助:编写输入一个字符串,计算其中的空格的个数. -
斐残璇美: 针对你的程序进行了修改#include <stdio.h>#include<string.h> int main() { char ch[80];//这里需要定义一个数组 int i,n,s=0; printf("请输入一串字符串:\n"); gets(ch);//为了输入空格,必须要用gets输入 n=strlen(ch);//计算字符串长度 for(i=0;i<n;i++) if(ch[i]==32 )//空格的ASCII码值为32 s=s+1;//不能用i求和,因为i用在了for循环 printf("%d\n",s); }

七台河市15117421804: excel中如何统计一个字符串中的空格 -
斐残璇美: 输入以下公式 =LEN(A2)-LEN(SUBSTITUTE(A2," ","")) 公式表示:以LEN分别求得A2单元格字符串的长度,和将空格替换为无后字符串的长度,差值即是空格的个数.详见附图

七台河市15117421804: 求c#一道题的写法,输入一行字符串,分别统计出其中英文字母、数字、空格的个数. -
斐残璇美: string s="abcd 135f"; char[] ch=s.ToCharArray(); int count1=0,count2=0;count3=0 for(int i=0;i<ch.Length;i++) {if(Char.IsLetter(ch[i])){count1++;continue;}if(char.IsNumber(ch[i])){count2++;continue;}else{count3++;continue;}...

七台河市15117421804: C语言编程题“从键盘任意输入一个字符串,统计其中出现空格和非空格的个数.” -
斐残璇美: 展开全部#include #include void main() { int a=0,b=0,i; char c[300]; printf("请输入一个少于300字符的字符串:"); gets(c); for(i=0;i<300,c[i]!='\0';i++) { if(c[i]==' ') a++; else b++; } printf("字符串中空格字符有%d个,非空格字符有%d个\n",a,b); } 谢谢采纳!

七台河市15117421804: vfp题:输入一个字符串,统计其中字母、数字、空格和其他字符的个数. -
斐残璇美: * vfp题:输入一个字符串,统计其中字母、数字、空格和其他字符的个数. SET TALK OFF CLEAR ACCEPT "请输入一个字符串:" TO _cstring STORE 0 TO _nzm,_nsz,_nkg,_nqt FOR _i=1 TO LEN(_cstring)_css=SUBSTR(_cstring,_i,1)...

七台河市15117421804: 从键盘输入一个字符串,分别统计其中每个数字、空格、字母及其他字符 -
斐残璇美: #include<stdio.h> void main() { char str[256]; int a,d,k,q,i;gets(str); a=d=k=q=i=0;while ( str[i] ) {if ( str[i]==' ' ) k++;else if ( (str[i]>='0')&&(str[i]<='9') ) d++;else if ( (str[i]>='a')&&(str[i]<='z') ) a++;else if ( (str[i]>='A')&&(str[i]<='Z') ) a++;else q++;} ...

七台河市15117421804: 任意输入一字符串,统计其中数字、英文字母及空格的个数 -
斐残璇美: #include "stdio.h" void main() { char *p,a[100]; int i,num=0,zimu=0,space=0; printf("请输入字符串:\n"); gets(a); p=a; for(;*p!='\0';p++) { if((*p>'a'&&*p<'z')||(*p>'A'&&*p<'Z')||*p=='a'||*p=='z'||*p=='A'||*p=='Z')zimu++; else if(*p>'0'&&*p<'9'||*p=='0...

七台河市15117421804: )输入一个字符串,统计其中字母、数字、空格和其他字符各多少个. -
斐残璇美: C++版本:#include #include using namespace std; void main() { string str; getline(cin,str,'\n'); cout int nch = 0, ndata = 0, nsp = 0, other = 0; char ch;for (int i=0;i { ch = str.at(i); if ((ch>='A' && ch='a' && ch { nch++; } else if (ch>='0' && ch { ndata++; }...

七台河市15117421804: JAVA 输入一个字符串,要求统计其中的字母、数字和空格的个数. -
斐残璇美: import java.util.Scanner;public class Test {public static void main(String[] args){Scanner sc = new Scanner(System.in);String enter = sc.nextLine();//获取用户输入的字符串int num = 0;//记录数字个数int chr = 0;//记录字母个数int spc = 0;//记...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网