C Program to convert a string from Uppercase to Lowercase
In this C Programming example, we will implement the program to convert a string from upper case to lower case and print the output on the screen.
1. String Conversion – Upper Case to Lower Case
String Case Conversion is a process to convert each character of a string to either Uppercase or to Lowercase characters. In this example, we will convert from uppercase string to lowercase using their ASCII values.
Example: Enter the string which needs to be converted : HAPPY BIRTHDAY The String in Upper Case is = happy birthday
Helpful topics to understand this program better are-
2. C Program to convert a string from upper case to lower case
Let’s discuss the execution(kind of pseudocode) for the program to convert a string from upper case to lower case in C.
- Initially, the program will prompt the user to enter the string that needs to be converted.
- The string is now passed to a function
void convertUpperToLower(). - In the
void convertUpperToLower()function,Forloop is used to convert the entered string into a lower case string. - If the string is in the upper case then convert the string into the lower case by adding 32 from their ASCII value.
- Finally, output the string on the console.
Let us now implement the above execution of the program to convert string from upper case to lower case.
#include <stdio.h>
#include <string.h>
char string[1000];
void convertUpperCaseToLower() {
int i;
for (i = 0; string[i] != '\0'; i++) {
if (string[i] >= 'A' && string[i] <= 'Z') {
string[i] = string[i] + 32;
}
}
}
int main() {
printf("\nEnter the string : ");
gets(string);
convertUpperCaseToLower();
printf("\nThe String in Lower Case = %s", string);
return 0;
}
Note: The above program can also be implemented using a while loop as well.
Output Enter the string : UPPER TO LOWER String in Lower Case = upper to lower
3. Conclusion
In this C Programming example, we have discussed how to convert a string from upper case to lower case using the ASCII values.
Helpful Links
Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.
Also for the example C programs please refer to C Programming Examples.
All examples are hosted on Github.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?