This article is explained with an example (the novice wrote it for the first time, only to share some methods and skills he thought of in writing code. There are still many shortcomings, and I hope it can be useful to you)
Title Requirements: there is an article with multiple lines of text (no more than 10 lines), and the last line is end. It is required to count the number of English uppercase letters, lowercase letters, spaces and other characters respectively. The statistical results do not include the end of the last line.
first, Because scanf() and gets() still have some shortcomings (gets stops reading when it encounters' \ n 'and automatically adds' \ 0', scanf stops reading when it encounters a space character or carriage return character and stores the closing character '\ 0'), it may lead to the inability to read spaces and line breaks when inputting a character. In order to solve this problem, I wrote a character reading function instead of gets and scanf.
In the function, a character array is used to save a single character read by getchar(). As long as flag==1, characters are continuously input, and as long as specific conditions are set to make flag=0, input can be stopped.
About how to judge whether a specific string (word) stops input, you can define a string to store the stop character (e.g. end here). How to input this word during reading? I compare the input string with the stop string one by one (some time-consuming, but effective), compare the last character entered by judging with the penultimate character of the stop string (the penultimate character is' \ 0 '). If it is the same, continue to judge other characters in reverse order. As long as there are the same characters, make flag=0. If there are different characters, make flag=1. At the end of judgment, if flag=1, do not enter the stop string, continue to execute the input operation, otherwise stop the input.
Note: when this function judges whether the stop character is input, it judges only after the relevant characters have been input. Therefore, there will be stop characters saved at the end of the character array, and many times the stop characters are not what we want. Therefore, this part needs to be ignored or deleted in other ways during the next processing, I have taken the following code as an example. The number of letters needs to be counted, but end is not within the statistical range. Therefore, when judging characters one by one, the stop string part needs to be ignored. Here, I set the termination condition to n-4 (that is, except for the stop character and the \ 0 at the end). When writing code in each bit, I have to judge according to my own situation.
#include "stdio.h" #include "string.h" void count(char *s); void get(char *s); int main(void) { char a[80]; get(a); count(a); return 0; } void get(char *s) { int i=0,j,k=2,flag=1; char stop[4]="end"; while(flag==1) { s[i]=getchar(); if(s[i]==stop[2]) { for(j=i;j>i-3;j--) { if(s[j]==stop[k]) { flag=0; } else { flag=1; } k--; } k=2; } i++; } } void count(char *s) { int n,i,ALPHA=0,alpha=0,num=0,other=0,space=0; n=strlen(s); for(i=0;i<n-4;i++) { if(s[i]==' ') { space++; } else if(s[i]>='a'&&s[i]<='z') { alpha++; } else if(s[i]>='A'&&s[i]<='Z') { ALPHA++; } else if(s[i]>='0'&&s[i]<='9') { num++; } else { if(s[i]!='\n') { other++; } } } printf("Capital letters have%d individual\n Lowercase letters have%d individual\n Number yes%d individual\n There are spaces%d individual\n Other characters are%d individual\n",ALPHA,alpha,num,space,other); }