使用 C 程式將母音字母從大寫轉換為小寫或小寫轉換為大寫
一系列字元稱為字串。
宣告
以下是陣列宣告 −
char stringname [size];
例如 − char a[50]; 字串長度為 50 個字元
初始化
- 使用單個字元常量 −
char a[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
- 使用字串常量 −
char a[10] = "Hello":;
訪問
有一個控制字串 "%s" 用於訪問字串,直到遇到 ‘\0’。
用於將母音字母從大寫轉換為小寫或從小寫轉換為大寫的邏輯是 −
for(i=0;string[i]!='\0';i++){ if(string[i]=='a'||string[i]=='e'||string[i]=='i'||string[i]=='o'||string[i]=='u'){ string[i]=toupper(string[i]); } } printf("The result string with converted vowels is : "); puts(string);
程式
以下是使用轉換函式將大寫字串轉換為小寫字串的 C 程式 −
#include<stdio.h> #include<ctype.h> void main(){ //Declaring variable for For loop (to read each position of alphabet) and string// int i; char string[40]; //Reading string// printf("Enter the string : "); gets(string); //For loop to read each alphabet// for(i=0;string[i]!='\0';i++){ if(string[i]=='a'||string[i]=='e'||string[i]=='i'||string[i]=='o'||string[i]=='u'){ string[i]=toupper(string[i]); } } printf("The result string with converted vowels is : "); puts(string); }
輸出
當執行上述程式時,它將產生以下結果 −
Run 1: Enter the string : TutoRialsPoint The result string with converted vowels is : TUtORIAlsPOInt Run 2: Enter the string : c programming The result string with converted vowels is : c programming
廣告