- 透過示例學習 C 時間
- 透過示例學習 C - 主頁
- C 示例 - 簡單程式
- C 示例 - 迴圈/迭代
- C 示例 - 樣式
- C 示例 - 陣列
- C 示例 - 字串
- C 示例 - 數學
- C 示例 - 連結串列
- C 程式設計實用資源
- 透過示例學習 C - 快速指南
- 透過示例學習 C - 資源
- 透過示例學習 C - 討論
用字串編寫的計算母音的程式
實現
現在,我們將會看到本程式的實際實現 −
#include <stdio.h>
int main() {
char s[] = "TajMahal"; // String Given
int i = 0;
int vowels = 0; // Vowels counter
int consonants = 0; // Consonants counter
while(s[i++] != '\0') {
if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' )
vowels++;
else
consonants++;
}
printf("'%s' contains %d vowels and %d consonants.", s, vowels, consonants);
return 0;
}
輸出
本程式的輸出應該是 −
'TajMahal' contains 3 vowels and 5 consonants.
string_programs_in_c.htm
廣告