An array of characters is called a string.
Declaration
Following is the declaration for an array −
char stringname [size];
For example − char a[50]; string of length 50 characters
Initialization
- Using single character constant −
char a[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
- Using string constants −
char a[10] = "Hello":;
Accessing
There is a control string "%s" used for accessing the string till it encounters ‘\0’.
The logic used to convert vowels from upper to lower or lower to upper is −
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);
Program
Following is the C program using conversion functions to convert an Upper case string to Lower case string −
#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); }
Output
When the above program is executed, it produces the following result −
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