如何使用 C 語言從給定的字串中刪除母音?
刪除給定字串中母音的實現邏輯如下 -
for(i=0; i<len; i++) //repeat until i<len{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || //checking to delete the vowels
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U'){
for(j=i; j<len; j++){
str[j]=str[j+1];
}
len--;
}
}示例
以下是用於刪除給定字串中母音的 C 程式 -
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(){
char str[20];
int len, i, j;
clrscr();
printf("Please Enter any String: ");
gets(str);
len=strlen(str);
for(i=0; i<len; i++){
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U'){
for(j=i; j<len; j++){
str[j]=str[j+1];
}
len--;
}
}
printf("After Deleting the vowels, the String is: %s",str);
getch();
}輸出
當執行以上程式時,它會生成以下結果 -
Please Enter any String: TutorialsPoint After Deleting the vowels, the String is: TtralsPint
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP