天天看點

C語言(十六):習題練習-字元串中字母統計

作者:LearningYard學苑
C語言(十六):習題練習-字元串中字母統計

分享興趣,傳播快樂,

增長見聞,留下美好!

親愛的您,這裡是LearningYard新學苑。

今天小編為大家帶來的是

C語言(十六):習題練習-字元串中字母統計。

Sharing interests,

Spread happiness,

Growth insights,

Leave behind the beauty!

Dear you,

This is the LearningYard New School.

What the editor brings to you today is

C language (16):

Exercise Exercise - Count Letters in a String.

一、思維導圖

此推文關于習題練習系列的内容主要如下:

The main content of this tweet about the exercise series is as follows:

C語言(十六):習題練習-字元串中字母統計

通過一些列習題的練習,進行思維和能力上的提升。

By practicing a series of exercises, improve your thinking and abilities.

二、解題思路

題目:

Title:

C語言(十六):習題練習-字元串中字母統計

要判斷字元串的每一個字元,需要使用數組來存儲。再通過循環判斷,哪些字母存在,将存在的數組個數又令為另一個數組。這道題重要的在于數組與循環結構的組合運用。

To determine each character of a string, an array needs to be used to store it. By looping through the loop, determine which letters exist and convert the number of existing arrays to another array. The important aspect of this question lies in the combination of arrays and loop structures.

三、代碼分析

1、輸入資料并接收資料

1. Input data and receive data

C語言(十六):習題練習-字元串中字母統計

2、使用for語句進行循環輸出,判斷字元串中小寫字母,并計算個數

2. Use the for statement for loop output, determine the lowercase letters in the string, and calculate the number of them

C語言(十六):習題練習-字元串中字母統計

3、按順序輸出小寫字母及其個數

3. Output lowercase letters and their numbers in order

C語言(十六):習題練習-字元串中字母統計

四、解題代碼

#include <stdio.h>

#define BUFFER_SIZE 80

#define COUNT_SIZE 26

int main( )

{

char str[BUFFER_SIZE+1];//從0到80,數組可存儲的字元為81個

int count[COUNT_SIZE]={0};

int i = 0;

gets(str); //向數組裡面輸入資料

for(i=0; str[i]!='\0'; i++)

{

if((str[i]>='a') && (str[i]<='z'))

count[str[i]-'a']++;//計算相對應字母的個數

}

for(i=0; i<COUNT_SIZE; i++)

{

if(count[i]>0) //字元串裡面存在的字母被輸出

{

printf("%c=%d\n", i+'a', count[i]);

}

}

return 0;

}

結語:C語言的學習需要配合不斷地練習,對于推文有什麼需要改進的地方,代碼錯誤的地方還請大家提出來。

Conclusion: The editor thinks that the pointer part is difficult to understand, so if there is any problem in the article, please ask everyone to raise it.

今天的分享就到這裡了。

如果您對今天的文章有獨特的想法,

歡迎給我們留言,

讓我們相約明天,

祝您今天過得開心快樂!

That's all for today's sharing.

If you have unique ideas about today's article,

please leave us a message.

Let's meet tomorrow.

I wish you a happy day!

翻譯:百度翻譯

參考:《零基礎入門學習C語言:帶你學C帶你飛》、哔哩哔哩小甲魚視訊、網絡圖檔

聲明:本文由LearningYard新學苑原創,若有侵權請聯系删除

文案&排版:易春秀

稽核:闫慶紅