Here is a C program to convert uppercase alphabets to lowercase in a string. We first take a string as input from user. We have to convert all uppercase alphabets of input string to lowercase alphabets.
For Example
Input String: ApPLe
Output String: apple
C program to convert uppercase to lowercase using strlwr function
This program first takes a string as input from user using gets function and stores it in a character array 'inputString'. It calls strlwr function by passing inputString and prints it's response on screen.
Function strlwr is used to convert all characters of a string to lowercase. Here is the declaration for strlwr function.
#include <stdio.h> #include <string.h> int main(){ char inputString[100]; int index; printf("Enter a String\n"); gets(inputString); printf("String with all lowercase characters \n%s ", strlwr(inputString)); return 0; }Output
Enter a String TechCrashCourse String with all lowercase characters techcrashcourse
C program to convert uppercase to lowercase using isupper and tolower function
Function isupper check whether the passed character is uppercase letter or not. It returns a non zero value(true) for uppercase otherwise zero(false).
Function tolower converts uppercase alphabets to lowercase. If passed character(c) is uppercase, it returns lowercase equivalent to c, otherwise c remain unchanged.
Functions isupper and tolower are defined in ctype.h header file.
Here is the declaration for isupper() and tolower() function.
int isupper(int c);
int tolower(int c);
#include <stdio.h> #include <ctype.h> int main(){ char inputString[100]; int index; printf("Enter a String\n"); gets(inputString); for(index=0; inputString[index] != '\0'; index++){ if(isupper(inputString[index])){ inputString[index] = tolower(inputString[index]); } else { inputString[index] = inputString[index]; } } inputString[index] = '\0'; printf("String with all lowercase characters\n%s", inputString); return 0; }Output
Enter a String C Programming Is Awesome String with all lowercase characters c programming is awesome
C program to convert uppercase string to lowercase string using user defined function
Here we are using two user defined functions, isUpperCase() and toLowerCase(). isUpperCase function returns one if passed character is uppercase character otherwise zero. toLowerCase returns lowercase character corresponding to upperCase character c.
#include <stdio.h> int isUpperCase(char c); int toLowerCase(char c); int main(){ char inputString[100]; int index; printf("Enter a String\n"); gets(inputString); for(index=0; inputString[index] != '\0'; index++){ if(isUpperCase(inputString[index])){ inputString[index] = toLowerCase(inputString[index]); } else { inputString[index] = inputString[index]; } } inputString[index] = '\0'; printf("String with all lowercase characters \n%s", inputString); return 0; } int isUpperCase(char c){ if(c >= 'A' && c <= 'Z') return 1; else return 0; } int toLowerCase(char c){ return c + (32); }Output
Enter a String C ProGraMMing Is AweSome String with all lowercase characters c programming is awesome
Related Topics