当前位置:首页 » 编程语言 » c语言如何统计英文数字个数
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言如何统计英文数字个数

发布时间: 2022-04-17 23:44:24

⑴ 救命啊:用c语言编程“任意输入一行英文字母,统计出其中英文字母的个数 ”怎么弄

#include <stdio.h>
void main()
{
char c;
int letter=0;
printf("请输入一行字符:\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letter++;
}
printf("英文字母的个数:%d\n",letter);
}

⑵ C语言问题: 输入一行字符,分别统计英文字符、数字字符、空格和其它字符的个数

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

int main() {

char s[100];

int countLetter = 0;

int countNum = 0;

int countSpace = 0;

int countOther = 0;

printf("请输入一个字符串:");

scanf("%[^ ]",s);

for(int i = 0;i< strlen(s);i++){

if((*(s+i)>='A'&&*(s+i)<='Z')||(*(s+i)>='a'&&*(s+i)<='z')) {

countLetter++;

continue;

}

if(*(s+i)>='0'&&*(s+i)<='9') {

countNum++;

continue;

}

if(*(s+i)==' ') {

countSpace++;

continue;

}

else

countOther++;

}

printf("字母有:%d个 数字有:%d个 空格有:%d个 其他字符有:%d个",

countLetter,countNum,countSpace,countOther);

return 0;

}

⑶ c语言统计大小写字母 数字个数

#include <stdio.h>

#include <stdlib.h>

#define N 100

void func3()

{

char str[N];

int i,lower=0,upper=0,digit=0,space=0;

long others=0;

printf("Input a string:");

gets(str);

for(i=0;str[i]!='';i++)

{

if(str[i]>='a' && str[i]<='z')

lower++; /*统计小写英文字母*/

else if(str[i]>='A' && str[i]<='Z')

upper++; /*统计大写英文字母*/

else if(str[i]>='0' && str[i]<='9')

digit++; /*统计字符串*/

else if(str[i]==' ')

space++;

else

others++; /*统计其他字母*/

}

printf("lower English character:%d ",lower);

printf("upper English character:%d ",upper);

printf("digit character:%ld ",digit);

printf("space:%d ",space);

printf("other character: %ld ",others);

return 0;

}

int main()

{

while(1)

{

func3();

printf(" ");

system("pause");

}

return 0;

}

(3)c语言如何统计英文数字个数扩展阅读:

程序实现思路分析

统计大小写字母、数字的个数,首先要判断出字符是属于哪一种,然后增加计数。

1、判断

小写字母的范围为:'a'~'z'

大写字母的范围为:'A'~'Z'

数字的范围为:'0'~'9'

2、声明三个int变量并赋值初值为0

lower——统计小写英文字母

upper——统计大写英文字母

digit——统计数字

⑷ C语言统计英语单词的个数

C语言:统计输入的一行英文句子中的字母及单词个数,带注解!

//通过键盘输入一行英文句子,统计其中的英文字母和单词的数量,单词之间用空格分开(标点符号不算单词);
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
char string[100];//根据拟从键盘输入的字串的长度需要适当调整,要避免输入的长度超出设定的范围。
char c;
int i, num=0,sum=0,word=0; //定义 word 用来指示一个单词是不是结束或新单词是否开始;
printf("请从键盘输入一行需要查询的英文句子,进行单词数量统计: ");
gets(string); //从键盘获得输入的字符串;
//以下统计句子中的英文字符个数;
for(i=0;(c=string[i])!='';i++) //for循环语句,遍历句子中的每个字符;初始化i=0;若字符c!='',即未到达结束符''的话,执行i++;
{
if(('A'<=string[i]&&string[i]<='Z')||('a'<=string[i]&&string[i]<='z'))
sum++; //以上为条件句,如果字符在A~Z,a~z 范围之内的话,则执行sum++,累加英文字母个数;
}
//以下统计句子中的英文单词个数;
for(i=0;(c=string[i])!='';i++) //for循环语句,遍历句子中的每个字符;初始化i=0;若字符c!='',即未到达结束符''的话,执行i++;
{ //''用作字符串的结束符。它的ASCII数值是0。
if(c<'A'||c>'Z'&&c<'a'||c>'z') //设定条件:如果字符 c 遇到A~Z和a~z范围之外其它符号字符的话,包括遇到空格' ';
word=0; //上面条件为真时,执行这里,置word=0,表示未遇到单词,或,一个单词已结束,同时也意味着要开始遇到下一个新单词;
else if(word==0) //当条件(word==0)为真,执行下面花括号里面的语句;当word==0时,表示未遇到字母,即未遇到单词,或上一个单词已结束;
{
word=1; //那么置word=1,即,表示下一个新单词开始,
num++; //执行num++,累加英文单词的个数;
}
}
printf(" ");
printf("您输入的这句英文句子中共包含%d个英文字符,%d个英文单词。 ",sum,num);

}

示例:输入语句: Hello ! My friend , how are you ? (注意单词的前后有空格)

⑸ c语言中怎样统计字符串中包含英文字母的个数

c语言中要统计字符串中包含英文字母的个数可以参考以下内容:

main()

{

char str[100],*p;

int num[4],i;

p=str;

gets(str);

for(i=0;i<4;i++)

num[i]=0;

for(;*p!='';p++)

{

if((*p<='z'&&*p>='a')||(*p<='Z'&&*p>='A')) num[0]++;

else if(*p==' ') num[1]++;

else if((*p<='9'&&*p>='0')) num[2]++;

else num[3]++;

}

printf("%d %d %d %d ",num[0],num[1],num[2],num[3]);

}

(5)c语言如何统计英文数字个数扩展阅读:

在写代码的过程中需要注意:

void main()的用法并不是任何标准制定的。 C语言标准语法是int main,任何实现都必须支持int main(void) { /* ... */ }和int main(int argc, char* argv[]) { /* ... */ }。

类似于a+=a++;或者(i++)+(i++)+(i++)属于未定义行为,并不是说c语言中还未定义这种行为,它早有定论,它的结果取决于编译器实现,不要写这样的代码。

⑹ C语言:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数

#include<stdio.h>
voidmain()
{
intletter,space,digit,other;
charch;
letter=space=digit=other=0;
while((ch=getchar())!=' ')
{
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
letter++;
elseif(ch>='0'&&ch<='9')
digit++;
elseif(ch=='')
space++;
else
other++;
}
printf("字母:%d ",letter);
printf("空格:%d ",space);
printf("数字:%d ",digit);
printf("其它字符:%d ",other);
}

⑺ 如何用c语言编写一段可统计出所输入的一段字符中的英文字母、空格、数字和其他字符的个数

编程为:

#include<stdio.h>

intmain(){charc[50];inti,el=0,sp=0,nu=0,other=0;gets(c);//输入字符串for(i=0;i<strlen(c);i++)//strlen返回字符串长度{if((c[i]>='A'&&c[i]<='Z')||(c[i]>='a'&&c[i]<='z'))el++;elseif(c[i]>='0'&&c[i]<='9')nu++;elseif(c[i]=='')sp++;elseother++;}printf("英文字母个数=%d 数字个数=%d 空格字数=%d 其他字符个数=%d ",el,nu,sp,other);return0;}