当前位置:网站首页>统计字符串中各类字符的个数
统计字符串中各类字符的个数
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,字符数组的数组名是字符串首地址,是一个常量,不可以对其进行运算,字符串指针是一个指针变量,可以进行运算,因此可以更灵活的操作字符串,提高字符串的处理效率。
(如果有任何问题,可以在下方评论留言,谢谢)
边栏推荐
猜你喜欢

Openfeign facile à utiliser

Installing Oracle database 19C RAC on Linux

Solid principle: explanation and examples

文件上传-upload-labs

k8s入门:Helm 构建 MySQL

Rotating linked list (illustration)

Jumping | Blue Bridge Cup

使用wireshark抓取Tcp三次握手

顺序表基本功能函数的实现

Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
随机推荐
双向链表的实现(双向链表与单向链表的简单区别联系和实现)
First week of JS study
OpenFeign 簡單使用
idea中注释代码取消代码的快捷键
Call Stack
Routing foundation - dynamic routing
Sqli labs level 8 (Boolean blind note)
程序猿学英语-指令式编程
Kubesphere virtualization KSV installation experience
Tensorflow2 keras classification model
Minecraft模组服开服
STM32 new project (refer to punctual atom)
What is SQL injection
Programming ape learning English - imperative programming
web安全--逻辑越权
OpenFeign 简单使用
Shortcut key to comment code and cancel code in idea
HCIA - application layer
c语言将字符串中的空格替换成%20
HCIA - data link layer