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

c语言用程序统计英文字母数

发布时间: 2022-06-14 03:51:10

A. c语言 编写程序,从键盘输入若干个英文字母,并统计各字母出现的次数

#include
#include
#define
max
100
int
main()
{
char
str[max];
//
输入的字符串,最大长度是max-1,因为有一个字符串结束符
int
i
=
0,
count[52]
=
{
0
};
//
count
数组用来存储各个字母出现的次数
scanf("%s",
str);
while(str[i]
!=
'\0')
{
if
(str[i]
>=
'a'
&&
str[i]
<=
'z')
{
//
统计小写字母
count[str[i]
-
97
+
26]++;
}
if
(str[i]
>=
'a'
&&
str[i]
<=
'z')
{
//
统计大写字母
count[str[i]
-
65]++;
}
i++;
}
for
(i
=
0;
i
<
26;
i++)
{
//
输出大写字母信息
if
(count[i]
!=
0)
printf("%c\t%d\n",
i
+
65,
count[i]);
//
只输出不为零的数据
}
for
(i
=
26;
i
<
52;
i++)
{
//
输出小写字母信息
if
(count[i]
!=
0)
printf("%c\t%d\n",
i
+
97
-
26,
count[i]);
}
return
0;
}

B. 救命啊:用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. C语言编程:输入一串英文字母,统计每个字母(不区分大小写)出现的次数

#include<iostream>

#include<string>

using namespace std;

int main()

{

char str[50];

int n=0;

char *p;

p=str;

cout<<"请输入字符串:"<<endl;

cin>>str;

for(int i=0;*p!='';p++)

{

if(('a'<=*p && *p<='z') || ('A'<=*p && *p<='Z'))

{

n++;

cout<<*p<<" ";

}

else

break;

}

cout<<"字符串的数量为:"<<n<<endl;

return 0;

}

(3)c语言用程序统计英文字母数扩展阅读:

printf用法:

printf()函数的调用格式为:printf("&lt;格式化字符串&gt;",&lt;参量表&gt;)。

其中格式化字符串包括两部分内容:一部分是正常字符,这些字符将按原样输出;另一部分是格式化规定字符,以"%"开始,后跟一个或几个规定字符,用来确定输出内容格式。

参量表是需要输出的一系列参数,其个数必须与格式化字符串所说明的输出参数个数一样多,各参数之间用","分开,且顺序一一对应,否则将会出现意想不到的错误。

比如:

inta=1234;

printf("a=%d\n",a);

输出结果为a=1234。

D. 任意输入一行英文字符,统计出其中英文字母的个数。 跪求用C语言编程!

#include
int
main(){
char
c[50];
int
i,el=0,sp=0,nu=0,other=0;
gets(c);//输入字符串
for(i=0;
i
='a'
&&
c[i]<='z')||(c[i]>='a'
&&
c[i]<='z'))
el++;
else
if(c[i]>='0'&&c[i]<='9')
nu++;
else
if(c[i]=='
')
sp++;
else
other++;
}
printf("英文字母个数=%d\n数



=%d\n空



=%d\n其他字符个数=%d\n",el,nu,sp,other);
return
0;}已经测试过了,测试结果如下,有问题可以继续追问。

E. C语言简单编程(字母数统计)

#include<stdio.h>
int counte;
int counts;
int countn;
int counto;
void count(char a[])
{
int i;
counte=0;
counts=0;
countn=0;
counto=0;
for(i=0;a[i]!='\0';i++)
{
if(a[i]>='0'&&a[i]<='9')
countn++;
else if(a[i]==' ')
counts++;
else if(a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z')
counte++;
else
counto++;
}

}
main()
{
char str[100];
gets(str);
count(str);
printf("e=%d,s=%d,n=%d,o=%d",counte,counts,countn,counto);
}

其中 count函数的 else if(a[i]==' ')写错了,这个才是,‘=’是赋值,==才是比较。

F. 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]);

}

(6)c语言用程序统计英文字母数扩展阅读:

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

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

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

G. C语言编程:输入一行字符,统计其中英文字母的个数

#include<stdio.h>

int main()

{char s[200];

int i,n=0;

gets(s);

for(i=0;s[i];i++)

if(s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]<='z')n++;

printf("%d ",n);

getch();

return 0;

}

H. C语言编程,用while语句,输入一行字符统计字母的个数

代码如下:

#include <stdio.h>

int main()

{

char c;

int letters=0,space=0,digit=0,other=0;

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

while ((c=getchar())!=' ')

{

if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z')

{

letters++;

}

else if (c == ' ')

{

space++;

}

else if (c >= '0'&&c <= '9')

{

digit++;

}

else

{

other++;

}

}

printf("字母数:%d 空格数:%d 数字数:%d 其他字符:%d ",letters,space,digit,other);

return 0;

}

(8)c语言用程序统计英文字母数扩展阅读

while的执行顺序

while 循环的执行顺序非常简单,它的格式是:

while (表达式)
{
语句;
}

当表达式为真,则执行下面的语句;语句执行完之后再判断表达式是否为真,如果为真,再次执行下面的语句;然后再判断表达式是否为真……就这样一直循环下去,直到表达式为假,跳出循环。这个就是 while 的执行顺序。

注意,初学者编程时,if、else、for、while、do 后面的执行语句不论有多少行,就算只有一行也要加“{}”,养成良好的编程习惯尤为重要。

再来看一下 for 循环的格式:

for (表达式1;表达式2;表达式3)

在 for 循环的格式中,表达式 1、表达式 2 和表达式 3 在 while 循环中一个也不少,只不过不像 for 循环那样写在一起,而是分开写。在 while 循环中,循环变量 i 在定义的时候就给它赋初值,++i 则是写在 while 的循环体内。只有循环判断表达式与 for 一样,都是写在其后的括号中。

并且所有的 for 循环都可以转化成 while 循环,不仅如此,所有的 while 循环也都可以转化成 for 循环,for 循环和 while 循环可以相互转换。

I. C语言编程:输入一串字母,统计每个字母出现的次数

C语言程序如下:

#include<stdio.h>

int main()

{

char a[100];

char b[24];

int s[100] = { 0 };//用于存储字符的个数

gets(a);//输入字符

//开始比较

for (int x = 0; x < 24; x++)

{

int c = 0;//记录每个字符个数

b[x] = x + 97;//为了让b[0]是a,b[1]是b依次类推

for (int i = 0; i < 100; i++)

{

if (b[x] == a[i])

{

++c;

s[x] = c;

}

}

if (s[x]>=1)//只输出输入中有的字母 的个数

{

printf("%c %d ", b[x], s[x]);

}

}

getchar();

return 0;

}

(9)c语言用程序统计英文字母数扩展阅读:

程序思路:
分为三部分 首先输入字符串 ,其次设定一个字符数组英文小写字母24, 同时设一个int数组 记录个数, 以及一个int c 为了给int数组赋值。最后在输入的时候进行判断,如果字母的值 大于等于1才输出。

J. 用c语言编程,字符统计:输入一个文本文件,分别统计出其中英文字母、空格、数字和其它字符的个数

#include <stdio.h>

int main()

{

char c;

int letters=0,space=0,digit=0,other=0;

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

while ((c=getchar())!=' ')

{

if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z')

{

letters++;

}

else if (c == ' ')

{

space++;

}

else if (c >= '0'&&c <= '9')

{

digit++;

}

else

{

other++;

}

}

printf("字母数:%d 空格数:%d 数字数:%d 其他字符:%d ",letters,space,digit,other);

return 0;

}

运行效果:

(10)c语言用程序统计英文字母数扩展阅读

printf函数使用注意事项

1、域宽

%d:按整型数据的实际长度输出。

如果想输出指定宽度可以指定域宽,%md--&gt;m域宽,打印出来以后,在控制台上,显示m位;

如果我们要打印的数的位数如果超过我们设定m则原样输出;

如果我们要打印的数的位数如果小于我们设定的位数,则补空白,具体如下:

如果m为正数,则左对齐(左侧补空白);

如果m为负数,则右对齐(右侧补空白)。

2、转义字符

如果想输出字符"%",则应该在“格式控制”字符串中用连续两个%表示。

如:printf("%f%%",1.0/3);输出结果:0.333333%。