Home  >  Article  >  Backend Development  >  How to count the number of occurrences of each character in a string in C language?

How to count the number of occurrences of each character in a string in C language?

coldplay.xixi
coldplay.xixiOriginal
2020-07-07 14:04:2620299browse

C language counts the number of occurrences of each character in a string: first download and install winTC and open it; then shortcut key [ctrl N] to create a new file and enter the code; finally save the file and enter the statistics to be counted string and press the enter key.

How to count the number of occurrences of each character in a string in C language?

C language method to count the number of occurrences of each character in a string:

1. Download and install winTC And open

2, shortcut key ctrl N to create a new file, or click "File" - "New File"

3. Copy the following code to the editing area, as follows As shown in the picture

#include"stdio.h"
main()
{
int a[100]={0},i,j; 
char c;
while((c=getchar())!='\n')   /*获取字符并统计每个字母出现次数*/
for (i=65;i<=90;i++)
if(c==i||c==i+32) a[i]++ ;
for (j=65;j<=90;j++)  /*输出统计信息*/
if (a[j]>0) printf("%c:%-3d\n",j,a[j]);
getch();  /*保持命令提示窗口不被自动关闭*/
}

How to count the number of occurrences of each character in a string in C language?

4. Click the black button in the picture above, or the shortcut key ctrl F9; if

is not saved, the above After the operation, a save prompt window will pop up. Just enter the file name and save it. Remember the directory where you saved the file to facilitate searching for the compiled program. The default directory is: C:\Win-TC\projects

5. In the pop-up CMD window, enter the string to be counted and press the enter key. Display statistical results

How to count the number of occurrences of each character in a string in C language?

Related learning recommendations: C video tutorial

The above is the detailed content of How to count the number of occurrences of each character in a string in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn