移除字串中母音(C++)
下面這個 C++ 程式說明了如何從一個給定的字串中移除母音 (a、e、i、u、o)。在此上下文中,我們建立了一個新的字串並逐個字元處理輸入字串,如果找到母音則將其排除在新字串中,否則在字串末尾新增該字元,字串結束後我們將新字串複製到原始字串中。該演算法如下;
演算法
START Step-1: Input the string Step-3: Check vowel presence, if found return TRUE Step-4: Copy it to another array Step-5: Increment the counter Step-6: Print END
根據上述演算法,用 c++ 編寫的以下程式碼如下所示;
示例
#include <iostream> #include <string.h> #include <conio.h> #include <cstring> using namespace std; int vowelChk(char); int main(){ char s[50], t[50]; int c, d = 0; cout<<"Enter a string to delete vowels\n"; cin>>s; for(c = 0; s[c] != '\0'; c++) { // check for If not a vowel if(vowelChk(s[c]) == 0){ t[d] = s[c]; d++; } } t[d] = '\0'; strcpy(s, t); cout<<"String after delete vowels:"<<s; return 0; } int vowelChk(char ch){ if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U') return 1; else return 0; }
這個 C++ 程式從一個字串中刪除母音:如果輸入字串是 "ajaykumar",它的結果將是 "jykmr"。最後,我們得到一個沒有母音的字串。
輸出
Enter a string to delete vowels ajaykumar String after delete vowels:jykmr
廣告