2012-10-01 30 views
0

我想知道我是否可以尋求一些幫助。我正在用C編寫一個程序,寫出字符,單詞和元音的數量在字符串中(帶有幾個添加的打印語句)。我想弄清楚如何編寫一個循環遍歷字符串的代碼,並計算至少包含3個元音的單詞的數量。我覺得這是一個非常簡單的代碼,但它總是最難忘的東西。任何幫助?如何統計包含至少3個元音的字數

另外:作爲新的C,我怎麼能得到相同的結果,而使用功能int vowel_count(char my_sen[]),而不是使用中我主要的代碼?

如果這是一個有點混亂我的意思是因爲我的主已經包含代碼來計算我的輸入元音的數量,我怎麼能稍微轉移到這個函數的代碼,仍然在主要調用它?

#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 
#define SENTENCE 256 


int main(void){ 

char my_sen[SENTENCE], *s; //String that containts at most 256 as well as a pointer 
int words = 1, count = 0,vowel_word = 0; //Integer variables being defined 
int i,vowel = 0, length; //More definitions 
printf("Enter a sentence: ");//Input sentence 
gets(my_sen);//Receives and processes input 
length = strlen(my_sen); //Stores the length of the input within length 

for(i=0;my_sen[i] != '\0'; i++){ 
    if(my_sen[i]=='a' || my_sen[i]=='e' || my_sen[i]=='i' || my_sen[i]=='o' || my_sen[i]=='u' || //Loop that states if the input contains any of the following 
     my_sen[i]=='A' || my_sen[i]=='E' || my_sen[i]=='I' || my_sen[i]=='O' || my_sen[i]=='U') //characters(in this case, vowels), then it shall be 
     {                       //stored to be later printed 
      vowel++; 
     } 


    if(my_sen[i]==' ' || my_sen[i]=='!' || my_sen[i]=='.' || my_sen[i]==',' || my_sen[i]==';' || //Similar to the vowel loop, but this time 
     my_sen[i]=='?')                   //if the following characters are scanned within the input 
     {                      //then the length of the characters within the input is 
      length--;                   //subtracted 

        } 

} 


for(s = my_sen; *s != '\0'; s++){ //Loop that stores the number of words typed after 
    if(*s == ' '){    //each following space 
    count++; 
} 
} 


printf("The sentence entered is %u characters long.\n", length); //Simply prints the number of characters within the input 
printf("Number of words in the sentence: %d\n", count + 1); // Adding 1 to t[he count to keep track of the last word 
printf("Average length of a word in the input: %d\n", length/count);//Prints the average length of words in the input 
printf("Total Number of Vowels: %d\n", vowel);//Prints the number of vowels in the input 
printf("Average number of vowels: %d\n", vowel/count);//Prints the average number of vowels within the input 
printf("Number of words that contain at least 3 vowels: %d\n", vowel_word);//Prints number of words that contain at least 3 vowels 
return 0; 
} 
+0

如果您創建了類似於'type *'函數的函數來檢查元音,那麼第一個'if'語句中的條件可能會更具可讀性。第二個if語句也是如此 –

回答

1

1)獲取的字符串,

2)使用的strtok()來獲得由空間分隔每個字。

3)通過char逐字符遍歷每個字符串以檢查它是否是元音。

0

請檢查下面的代碼

#include<stdio.h> 
    #include <string.h> 

    int count_vowels(char []); 
    int check_vowel(char); 

    main() 
    { 
     char array[100]; 
     printf("Enter a string\n"); 
     gets(array); 
     char seps[] = " "; 
     char* token; 
     int input[5]; 
     int i = 0; 
     int c = 0; 
     int count = 0; 

     token = strtok (array, seps); 
     while (token != NULL) 
     { 
     c = 0; 
     c = count_vowels(token); 
     if (c >= 3) { 
      count++; 
     } 
     token = strtok (NULL, seps); 
     } 
     printf("Number of words that contain atleast 3 vowels : %d\n", count); 
     return 0; 
    } 

    int count_vowels(char a[]) 
    { 
     int count = 0, c = 0, flag; 
     char d; 

     do 
     { 
      d = a[c]; 

      flag = check_vowel(d); 

      if (flag == 1) 
      count++; 

      c++; 
     }while(d != '\0'); 

     return count; 
    } 

    int check_vowel(char a) 
    { 
     if (a >= 'A' && a <= 'Z') 
      a = a + 'a' - 'A'; /* Converting to lower case */ 

     if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') 
      return 1; 

     return 0; 
    } 
2

這不是太大的問題。

#include <ctype.h> 
#include <stdio.h> 
#include <string.h> 

int vowel_count(char my_sen[]) 
{ 
    int wcount = 0; // number of words with 3+ vowel chars 
    int vcount = 0; // current number of vowel chars in the current word 
    int i = 0; // index into the string 
    int ch; 
    while ((ch = my_sen[i++]) != '\0') 
    { 
    if (isspace(ch) || !isalpha(ch)) 
    { 
     // ch is not an alphabetical char, which can happen either 
     // before a word or after a word. 
     // If it's after a word, the running vowel count can be >= 3 
     // and we need to count this word in. 
     wcount += vcount >= 3; // add 1 to wcount if vcount >= 3 
     vcount = 0; // reset the running vowel counter 
     continue; // skip spaces and non-alphabetical chars 
    } 
    if (strchr("aeiouAEIOU", ch) != NULL) // if ch is one of these 
    { 
     ++vcount; // count vowels 
    } 
    } 
    // If my_sen[] ends with an alphabetical char, 
    // which belongs to the last word, we haven't yet 
    // had a chance to process its vcount. We only 
    // do that in the above code when seeing a non- 
    // alphabetical char following a word, but the 
    // loop body doesn't execute for the final ch='\0'. 
    wcount += vcount >= 3; // add 1 to wcount if vcount >= 3 
    return wcount; 
} 

int main(void) 
{ 
    char sen[] = "CONSTITUTION: We the People of the United States..."; 
    printf("# of words with 3+ vowels in \"%s\" is %d", sen, vowel_count(sen)); 
    return 0; 
} 

輸出(ideone):

# of words with 3+ vowels in "CONSTITUTION: We the People of the United States..." is 3 

順便說一句,你可以改變這個函數來計算,你需要所有的東西。它已經找到了單詞開始和結束的位置,所以簡單的單詞計數很容易實現。字長也一樣。等等。

+0

非常感謝您的支持,我特別感謝您提供的意見,以便我能夠從您的寫作中學習。再次,非常感謝並感謝您的洞察力。 – user1664272

+0

不客氣。順便說一句if(isspace(ch)||!isalpha(ch))'可以簡化爲'if(!isalpha(ch))'。 –

相關問題