[英]Words frequency with number of letters (basic loop only)
我想編寫 C 程序以使用基本循環技術通過字母數字打印單詞長度及其頻率。 我可以讓字長工作,但我堅持使用頻率(例如:Do 2 not 3 Judge 5 a 1 book 4(已經解決了這個問題))
ETC...
#include <stdio.h>
int main(void) {
char word[30];
int i = 0,b=0,c=0,j=0,d=0;
printf("Please enter a word: ");
for (i = 0; i < 30 ; i++){
scanf("%s", word);
while (word[b]!='\0'){
b++;
}
printf("%s %d ", word, b);
b = 0;
}
return 0;
}
你的問題並不完全清楚。 但是據我了解,您還想打印用戶輸入長度為“ l ”的單詞的次數(頻率)。 所以我會回答:
您可以將單詞的長度存儲在用戶輸入的數組中。 讀取所有輸入后,您可以從存儲的數組中打印每個字長的頻率
請參閱以下代碼以了解我的意思:
#include <stdio.h>
int main(void) {
char word[30];
int i = 0,b=0,c=0,j=0,d=0;
int word_length_freq[30]={0}; //an array which will store the frequency of word length(all initialized to 0)
//eg. if word is "hello" it will increase count of word_length_freq[5] by 1
printf("Please enter a word: ");
for (i = 0; i < 3 ; i++){
scanf("%s", word);
while (word[b]!='\0'){
b++;
}
word_length_freq[b]++;
printf("%s %d ", word, b);
b = 0;
}
for(int i=1;i<30;i++){ //This will print the frequency of all words from length 1 to 30
printf("There are %d words of length %d\n",word_length_freq[i],i);
}
return 0;
}
我希望這能解決你的問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.