首頁 > 後端開發 > C++ > 列印單字數量、元音字母數量以及每個字元的出現頻率

列印單字數量、元音字母數量以及每個字元的出現頻率

WBOY
發布: 2023-08-25 17:05:19
轉載
1450 人瀏覽過

列印單字數量、元音字母數量以及每個字元的出現頻率

输入一个字符串,找到单词的总数、元音字母的数量和用户输入的字符的频率

Input : enter s string : I love my MOM  
   Enter a charcter of which you want to find a frequency: M
   Total frequency of M : 2
   Total number of vowels : 4
   Total number of words : 4
登入後複製

算法

START
Step 1 Declare array of string, ch, i, freq to 0, vow to 0, word to 0
Step 2 Input a string and a character ch
Step 3 Loop for from i to 0 and str[i]!=’\o’ and ++i
Step 3.1 IF statement for ch==str[i]
   Post incrementing freq
   Step 3.2 End If
   Step 3.3 IF statement
   str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U'
      Post incrementing vow
   Step 3.4 End If
   Step 3.5 IF statement str[i]=’ ’
      Post incrementing word
   Step 3.6 End If
Step 4 End For loop
STOP
登入後複製

Example

#include <stdio.h>
int main() {
   char str[1000], ch;
   int i, freq=0, vow=0, word=0;
   printf("Enter a string of your choice: ");
   gets(str);
   printf("Enter a character of which you want to find the frequency: ");
   scanf("%c",&ch);
   for(i = 0; str[i] != &#39;\0&#39;; ++i){
      if(ch == str[i]) //to find the frequency of a character {
         ++freq;
      }
      if(str[i]==&#39;a&#39;||str[i]==&#39;e&#39;||str[i]==&#39;i&#39;||str[i]==&#39;o&#39;||str[i]==&#39;u&#39;||str[i]==&#39;A&#39;||str[i]==&#39;E&#39;||str[i]==&#39;I&#39;||str[i]==&#39;O&#39;||str[i]==&#39;U&#39;) {
         ++vow; //to find the number of vowels
      }
      if (str[i] == &#39; &#39;) {
         word++; //to find the number of words
      }
   }
   printf("Frequency of %c = %d", ch, freq);
   printf("</p><p>total number of vowels in a string are %d " ,vow );
   printf("</p><p>total number of words in a string are %d " ,word+1 );
   return 0;
}
登入後複製

输出

如果我们运行上面的程序,它将生成以下输出。

Enter a string of your choice: I love PrograMMIng
Enter a character of which you want to find the frequency: M
Frequency of M = 2
total number of vowels in a string are 6
total number of words in a string are 3
登入後複製

以上是列印單字數量、元音字母數量以及每個字元的出現頻率的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板