当前位置:网站首页>统计字符串中各类字符的个数
统计字符串中各类字符的个数
2022-07-02 06:32:00 【程序员在旅途】
统计字符串中各类字符的个数
一、题目描述
从键盘输入一行字符串,统计其中的大写字母、小写字母、空格、数字、和其他字符的个数。(更多内容,可参阅程序员在旅途)
二、分析解答
本题主要考察C语言字符串的相关知识点。字符串处理是C语言中很重要的一个知识点,但在C语言汇总并没有字符串类型,因此,只能采用字符数组或者字符指针的形式来使用字符串。要记住一点,不论我们使用的是字符串常量还是字符串变量,为了方便处理字符串,系统自动 给字符串加上一个结束标志’\0’(’\0’代表ASCII为0的字符,他不是可显示字符,只是一个空操作符,提供标志辨识功能,用它做结束标志不会产生附加的操作或者增加有效字符)。字符串在内存中连续存储,占用一块连续的空间。
代码如下:
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i=0,alpha=0,Alpha=0,digit=0,space=0,other=0;
char *str;
//输入N,确定字符串长度是多少;然后申请相应长度的地址空间并赋值给str。
scanf("%d",&n);
// 消去scanf函数 遗留下来的换行符 对统计的干扰
getchar();
if((str = (char *)malloc(n * sizeof(char))) == NULL){
printf("Not able to allocate memory");
exit(1);
}
//输入字符串。这里要注意,字符输入函数的选取,scanf因为不能接受空格,会导致最终统计结果的不准确,故在此选用gets()/getchar();
gets(str);
//统计字符个数。 字符串以'\0'为终止符在内存中连续存储的,
while(*(str+i) != '\0'){
if(*(str+i) >= 'A' && *(str+i) <= 'Z' ) { //大小字母
Alpha ++;
}else if( *(str+i) >= 'a' && *(str+i) <= 'z' ){ //小写字母
alpha ++;
}else if( *(str+i) >= '0' && *(str+i) <= '9' ){ // 数字
digit ++;
}else if( *(str+i) == ' ' ){ //空格
space ++;
}else{ //其他字符
other ++;
}
i++;
}
printf("alpha = %d \n Alpha = %d\n digit = %d \n space=%d\n other=%d \n ",alpha,Alpha,digit,space,other);
return 0;
}也可以使用下列方式:
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i=0,alpha=0,Alpha=0,digit=0,space=0,other=0;
char *str;
//输入N,确定字符串长度是多少;然后申请相应长度的地址空间并赋值给str。
scanf("%d",&n);
// 消去scanf函数 遗留下来的换行符 对统计的干扰
getchar();
if((str = (char *)malloc(n * sizeof(char))) == NULL){
printf("Not able to allocate memory");
exit(1);
}
//输入字符串。这里要注意,字符输入函数的选取,scanf因为不能接受空格,会导致最终统计结果的不准确,故在此选用gets()/getchar();
//也可以使用这个方式输入字符串
while(i<=n){
*(str+i) = getchar();
i++;
}
// 也可以根据输入的字符个数循环遍历字符串
for(i=0;i<n;i++){
if(*(str+i) >= 'A' && *(str+i) <= 'Z' ) { //大小字母
Alpha ++;
}else if( *(str+i) >= 'a' && *(str+i) <= 'z' ){ //小写字母
alpha ++;
}else if( *(str+i) >= '0' && *(str+i) <= '9' ){ // 数字
digit ++;
}else if( *(str+i) == ' ' ){ //空格
space ++;
}else{ //其他字符
other ++;
}
}
printf("alpha = %d \n Alpha = %d\n digit = %d \n space=%d\n other=%d \n ",alpha,Alpha,digit,space,other);
return 0;
}三、归纳总结
1,字符串处理在计算机中会经常遇到,因此,了解字符串的定义、初始化、输入输出、存储方式等至关重要。
2,C语言编译系统会自动的在字符串末尾添加’\0’辨识符作为字符串结束的标志,因此,在循环读取字符串中的字符时可以使用这个标志作为循环跳出的判断。
3,为了方便字符串的输入输出,C语言提供了除 scanf()之外的getchar()、gets()函数。可以方便我们对字符串进行输出输出。
4,字符数组的数组名是字符串首地址,是一个常量,不可以对其进行运算,字符串指针是一个指针变量,可以进行运算,因此可以更灵活的操作字符串,提高字符串的处理效率。
(如果有任何问题,可以在下方评论留言,谢谢)
边栏推荐
- OpenShift 容器平台社区版 OKD 4.10.0部署
- Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
- 类和对象(类和类的实例化,this,static关键字,封装)
- HCIA - data link layer
- Jz-061-serialized binary tree
- Minecraft安装资源包
- Introduction to anti interception technology of wechat domain name
- D interface and domain problems
- 用数字 5,5,5,1 ,进行四则运算,每个数字当且仅当用一次,要求运算结果值为 24
- When a custom exception encounters reflection
猜你喜欢

OpenFeign 簡單使用

c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)

Linux安装Oracle Database 19c RAC

commands out of sync. did you run multiple statements at once

Application of kotlin - higher order function

Linux二进制安装Oracle Database 19c

Honeypot attack and defense drill landing application scheme

IP协议与IP地址

Web security -- Logical ultra vires

When a custom exception encounters reflection
随机推荐
一、Qt的核心类QObject
Use Wireshark to grab TCP three handshakes
When a custom exception encounters reflection
PCL calculates the intersection of three mutually nonparallel planes
Sentinel easy to use
The best blog to explain the basics of compilation (share)
HCIA—應用層
Detailed explanation of NIN network
Shortcut key to comment code and cancel code in idea
Synchronize files using unison
Tcp/ip - transport layer
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
Loadbalancer dynamically refreshes Nacos server
Minecraft插件服开服
Googlenet network explanation and model building
Application of kotlin - higher order function
Kubernetes deploys Loki logging system
Installing Oracle database 19C RAC on Linux
[blackmail virus data recovery] suffix Hydra blackmail virus
OpenShift 容器平台社区版 OKD 4.10.0部署