|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
//有一片文章,共有3行文字,每行有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符得个数。
#include<stdio.h>
void main()
{
char a[3][80];
int i,n,k,b,c,d,e,f;
gets(a[k]);
for(i=0;i<3;i++)
{
for(k=0;k<80;k++)
{
if(a[k]>='48' &&a[k]<='57')
{
b++;
}
else
if(a[k]>='65' &&a[k]<='90')
{
c++;
}
else
if(a[k]>='97' &&a[k]<='122')
{
d++;
}
if(a[k]=='0')
{
e++;
}
else
f++;
}
}
printf("数字的个数是:%d\n",b);
printf("大写字母的个数是:%d\n",c);
printf("小写字母的个数是:%d\n",d);
printf("空格的个数是:%d\n",e);
printf("其他字符的个数是:%d",f);
}
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- void strFromFile(const char *Faddr, char *str, size_t sz)
- {
- FILE *fp = fopen(Faddr, "r");
- if (fp == NULL)
- {
- perror("file opend fail");
- exit(1);
- }
- fread(str, sizeof(char), sz, fp);
- fclose(fp);
- printf("文件内容:\n%s\n", str);
- int ct[5] = {0};
- for (char *i = str; *i != '\0'; i++)
- {
- if (isdigit(*i)) //统计数字
- {
- ct[0]++;
- }
- if (islower(*i)) //统计小写字母
- {
- ct[1]++;
- }
- if (isupper(*i)) //统计大写字母
- {
- ct[2]++;
- }
- if (isspace(*i)) //统计空格
- {
- ct[3]++;
- }
- if (ispunct(*i)) //统计除去数字,字母,空白字符以外的字符
- {
- ct[4]++;
- }
- }
- printf("\n数字的个数是:%d\n", ct[0]);
- printf("大写字母的个数是:%d\n", ct[2]);
- printf("小写字母的个数是:%d\n", ct[1]);
- printf("空格的个数是:%d\n", ct[3]);
- printf("其他字符的个数是:%d", ct[4]);
- }
- int main(int argc, char const *argv[])
- {
- char *addr = "E:/Users/admin/Documents/VScode/Code/tmp/text.txt";
- char str[255] = {0};
- strFromFile(addr, str, 255);
- return 0;
- }
复制代码
=======================================
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。
E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-zvxmiasy.a5t --stdout=Microsoft-MIEngine-Out-mw4zhur4.fpl --stderr=Microsoft-MIEngine-Error-w42cggib.yph --pid=Microsoft-MIEngine-Pid-kfg3knrd.cvb --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
文件内容:
0123456789
ASDFGHJKLL
asdfghjkll
!@#$%^&*()
数字的个数是:10
大写字母的个数是:10
小写字母的个数是:10
空格的个数是:3
其他字符的个数是:10
E:\Users\admin\Documents\VScode\Code>
|
|