Here is the C program to convert all lowercase alphabets to uppercase alphabets in a string. We first take a string as input from user. We have to convert all lowercase alphabets in a input string to uppercase alphabets.
For Example
Input String: Apple
Output String: APPLE
C program to convert lowercase to uppercase using strupr function
This program first takes a string as input from user using gets function and stores it in a character array 'inputString'. It calls strupr function by passing inputString and prints it's response on screen.
Function strupr is used to convert all characters of a string to uppercase. Here is the declaration for strupr function. Function strupr is defined inside string.h header file.
#include <stdio.h> #include <string.h> int main(){ char inputString[100]; int index; printf("Enter a String\n"); gets(inputString); printf("String with all uppercase characters\n%s", strupr(inputString)) return 0; }Output
Enter a String CProgramming String with all uppercase characters CPROGRAMMING
C program to convert lowercase to uppercase using islower and toupper function
Function islower check whether the passed character is lowercase letter or not. It returns a non zero value(true) for lowercase otherwise zero(false). Function toupper converts lowercase alphabets to uppercase. If passed character(c) is lowercase, it returns uppercase equivalent to c, otherwise c remain unchanged. islower and toupper functions are defined in ctype.h header file. Here is the declaration for islower() and toupper() function.
int islower(int c);
int toupper(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++){ /* Check if character in inputArray is lower Case*/ if(islower(inputString[index])){ /* Convert lower case character to upper case using toupper function */ inputString[index] = toupper(inputString[index]); } else { inputString[index] = inputString[index]; } } inputString[index] = '\0'; printf("String with all uppercase characters\n%s", inputString); return 0; }Output
Enter a String TechCrashCourse String with all uppercase characters TECHCRASHCOURSE
C program to convert lowercase string to uppercase string using user defined function
Here we are using two user defined functions, isLowerCase() and toUpperCase(). isLowerCase function returns one if passed character is lowercase character otherwise zero. toUpperCase returns uppercase character corresponding to lowerCase character c.
#include <stdio.h> int isLowerCase(char c); int toUpperCase(char c); int main(){ char inputString[100]; int index; printf("Enter a String\n"); gets(inputString); for(index=0; inputString[index] != '\0'; index++){ if(isLowerCase(inputString[index])){ inputString[index] = toUpperCase(inputString[index]); } else { inputString[index] = inputString[index]; } } inputString[index] = '\0'; printf("String with all uppercase characters\n%s", inputString); return 0; } int isLowerCase(char c){ if(c >= 'a' && c <= 'z') return 1; else return 0; } int toUpperCase(char c){ return c - 32; }Output
Enter a String TECHCrashCourse String with all uppercase characters TECHCRASHCOURSE
Related Topics