2014-12-13 98 views
-1

我試圖找出字符串中的元音數量,但我沒有找到未初始化數組長度的想法。我將遍歷所有的數組元素,並返回元音的否。我通過論壇並閱讀,有內置的strlen函數,但我想讓我自己的函數了解工作,不僅僅是我的工作done.i 「已經實現了這個使用for循環遍歷所有準備,但它是一個失敗..自定義函數來查找數組的長度?

char strin[]; 
printf("Check for the number of vowels in the string : \n"); 
scanf("%s" , &strin); 
int Number_of_vowels(strin); 


//a custom function to find the string length. 
int string_len(char[] string) 
{ 
    //code to find the length 
} 
//a custom function to do the check. 
int Number_of_vowels(char strin[]) 
{ 
    for(i = 0 ; i < stringlength , i++) 
    { 
     //check if the input char is vowel. 
    } 
} 
+0

如果數組未被初始化,那麼它沒有長度。 – csmckelvey 2014-12-13 17:59:14

+0

您必須爲Number_of_vowels添加一個長度參數。一個字符串的長度可以通過strlen(strin)來檢索; – noxmetus 2014-12-13 18:03:02

回答

0

使用scanf的返回值。

int stringlength; 
int vowels; 

stringlength = scanf("%s", &strin); 
vowels = Number_of_vowels(strin, stringlength); 

int Number_of_vowels(char strin[], int stringlength) 
{ 
    int vowels = 0; 
    int i; 
    for (i = 0; i < stringlength, i++) { 
     // Code testing for vowels (possibly a switch loop testing ASCII values) 
    } 

    return vowels; 
} 
+0

你還必須指定一個長度的嚴格陣列。你在上面的代碼中沒有意識到它:strin [] =「Hello World!...」C使strings []的長度爲+ 1.所以在這種情況下,你的輸入有效切割爲31個字符該數組將爲32且爲空字符)。如果你真的想要無限輸入,你將不得不一次使用緩衝區並讀取塊。最簡單的是循環STDIN輸入,直到達到EOF。 – Carlise 2014-12-13 23:26:59

1

對不起,因爲我無法評論,但我也對我的iPad上因此,格式化會很糟糕。但是,爲什麼當它傳遞到Number_of_Vowels()時,你只需要把它的長度設爲strin[]

無論如何,您已經通過strin[]數組,因此該數組的屬性也應該進入該方法。

+0

如果初始化,那麼用戶將失去輸入大號字符的能力......我希望char數組的限制更少。 – 2014-12-13 18:23:56

+0

@AkashChowdary,用戶最多可以鍵入31個字符,因爲數組的長度爲32 – 2014-12-14 03:27:29

+0

如果我沒有intialise是我的問題?如果我沒有初始化字符串數組怎麼辦? – 2014-12-15 11:51:37