当前位置:网站首页>Count the number of various characters in the string
Count the number of various characters in the string
2022-07-02 08:46:00 【Programmers on the road】
Count the number of various characters in the string
One 、 Title Description
Enter a line of string from the keyboard , Count the capital letters 、 Lowercase letters 、 Space 、 Numbers 、 And the number of other characters .( More , Please see the Programmers are on the road )
Two 、 Analysis answers
This paper mainly studies C Relevant knowledge points of language string . String processing is C A very important knowledge point in language , But in C Language summary has no string type , therefore , Strings can only be used in the form of character arrays or character pointers . Remember a little , Whether we use string constants or string variables , For the convenience of processing strings , System Automatically Add an end flag to the string ’\0’(’\0’ representative ASCII by 0 The characters of , It is not a displayable character , Just an empty operator , Provide sign recognition function , Using it as an end sign will not produce additional operations or add valid characters ). Strings are stored continuously in memory , Occupy a continuous space .
The code is as follows :
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i=0,alpha=0,Alpha=0,digit=0,space=0,other=0;
char *str;
// Input N, Determine the length of the string ; Then apply for the address space of corresponding length and assign it to str.
scanf("%d",&n);
// elimination scanf function Left over line breaks Interference with statistics
getchar();
if((str = (char *)malloc(n * sizeof(char))) == NULL){
printf("Not able to allocate memory");
exit(1);
}
// Input string . Pay attention here , Selection of character input function ,scanf Because spaces are not acceptable , It will lead to the inaccuracy of the final statistical results , Therefore, it is selected here gets()/getchar();
gets(str);
// Count the number of characters . String to '\0' Continuously stored in memory for terminators ,
while(*(str+i) != '\0'){
if(*(str+i) >= 'A' && *(str+i) <= 'Z' ) { // Big and small letters
Alpha ++;
}else if( *(str+i) >= 'a' && *(str+i) <= 'z' ){ // Lowercase letters
alpha ++;
}else if( *(str+i) >= '0' && *(str+i) <= '9' ){ // Numbers
digit ++;
}else if( *(str+i) == ' ' ){ // Space
space ++;
}else{ // Other characters
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;
}
You can also use the following methods :
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i=0,alpha=0,Alpha=0,digit=0,space=0,other=0;
char *str;
// Input N, Determine the length of the string ; Then apply for the address space of corresponding length and assign it to str.
scanf("%d",&n);
// elimination scanf function Left over line breaks Interference with statistics
getchar();
if((str = (char *)malloc(n * sizeof(char))) == NULL){
printf("Not able to allocate memory");
exit(1);
}
// Input string . Pay attention here , Selection of character input function ,scanf Because spaces are not acceptable , It will lead to the inaccuracy of the final statistical results , Therefore, it is selected here gets()/getchar();
// You can also enter a string in this way
while(i<=n){
*(str+i) = getchar();
i++;
}
// You can also cycle through the string according to the number of characters you enter
for(i=0;i<n;i++){
if(*(str+i) >= 'A' && *(str+i) <= 'Z' ) { // Big and small letters
Alpha ++;
}else if( *(str+i) >= 'a' && *(str+i) <= 'z' ){ // Lowercase letters
alpha ++;
}else if( *(str+i) >= '0' && *(str+i) <= '9' ){ // Numbers
digit ++;
}else if( *(str+i) == ' ' ){ // Space
space ++;
}else{ // Other characters
other ++;
}
}
printf("alpha = %d \n Alpha = %d\n digit = %d \n space=%d\n other=%d \n ",alpha,Alpha,digit,space,other);
return 0;
}
3、 ... and 、 Sum up
1, String processing is often encountered in computers , therefore , Understand the definition of string 、 initialization 、 Input and output 、 Storage mode is very important .
2,C The language compilation system will automatically add ’\0’ Identifier as a sign of the end of the string , therefore , This flag can be used to judge whether the loop jumps out when reading the characters in the string in a loop .
3, In order to facilitate the input and output of strings ,C The language provides the exception scanf() In addition to the getchar()、gets() function . It is convenient for us to output strings .
4, The array name of the character array is the first address of the string , Is a constant , It cannot be calculated , A string pointer is a pointer variable , It can be calculated , Therefore, you can operate strings more flexibly , Improve string processing efficiency .
( If there are any questions , You can comment below , thank you )
边栏推荐
猜你喜欢
随机推荐
[blackmail virus data recovery] suffix Hydra blackmail virus
gocv图片裁剪并展示
File upload Labs
Routing foundation - dynamic routing
选择排序和插入排序
Shortcut key to comment code and cancel code in idea
链表经典面试题(反转链表,中间节点,倒数第k个节点,合并分割链表,删除重复节点)
D interface and domain problems
ICMP Protocol
gocv图片读取并展示
Makefile基本原理
Linux安装Oracle Database 19c RAC
Gateway is easy to use
Sqli labs level 1
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
OpenShift 容器平台社区版 OKD 4.10.0部署
Openfeign facile à utiliser
IP协议与IP地址
File upload and download performance test based on the locust framework
Nacos 下载启动、配置 MySQL 数据库