C program to convert an uppercase string to lowercase – In this article, we will brief in on the multiple ways to convert an uppercase string to lowercase in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
In this piece, the ways used are as follows:
- Using Standard Method
- Using Function
- Using Recursion
- Using String Library Function
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As you can see in the image uploaded above, firstly, we need to enter a particular string in uppercase which we want to convert to lowercase.
The string entered here is: HELLO
Hence, the lowercase string, in this case, will be: hello
Thus, the different methods to convert a specific uppercase string to lowercase are as follows:
Using Standard Method
- Read the entered string which is in the upper case using gets(s) function.
2) The upper case alphabets ASCII value range is from 65 to 90.
3) For loop iterates with the structure for(i=0;s[i];i++)
If the ASCII value of any character of the string is in the range between 65 to 90,
Add 32 to the ASCII value of the character to convert into lower case.
4) After all iterations of for loop, we will get the string with lowercase alphabets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> #include <string.h> int main() { char s[1000]; int i; printf("Enter the string : "); gets(s); for(i=0;s[i];i++) { if(s[i]>=65 && s[i]<=90) s[i]+=32; } printf("string in lowercase ='%s'\n",s); return 0; } |
Output:
1 2 |
Enter the string: HELLO string in lowercase ='hello' |
Using Function
- The main() calls the stringlowercase() function to convert the uppercase alphabets of the string to lower case alphabets.
2) The stringlowercase() function checks each characters ASCII value of the given string. If any character’s ASCII value is in the range between 65 to 90 then add 32 to the ASCII value of the character. Then the character will be converted to lower case. Like this, the function converts all uppercase characters into lowercase.
3) Print the string which is having all lower case alphabets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> #include <string.h> void stringlowercase(char *s) { int i; for(i=0;s[i];i++) { if(s[i]>=65 && s[i]<=90) s[i]+=32; } } int main() { char s[1000]; int i; printf("Enter the string: "); gets(s); stringlowercase(s); printf("string in lowercase ='%s'\n",s); } |
Output:
1 2 |
Enter the string: GOUTHAM string in lowercase ='goutham' |
Using Recursion
- The main() calls the stringlowercase() function by passing the string as an argument.
2) If character available then stringlowercase() function checks the ASCII value of s[i] is in between 65 to 90 then add 32 to ASCII value of the character and the lower case character will be placed into s[i++]. The function calls itself by passing modified string s. It calls recursively until if (s[i]) becomes false.i.e there is no characters for checks.
3) Print the lower case string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> #include <string.h> int stringlowercase(char *s) { static int i=0; if(s[i]) { if(s[i]>=65 && s[i]<=90] ) s[i++]+=32; stringlowercase(s); } } int main() { char s[1000]; printf("Enter the string: "); gets(s); stringlowercase(s); printf("string in lowercase ='%s'\n",s); } |
Output:
1 2 |
Enter the string: PROGRAM string in lowercase ='program' |
Using String Library Function
- The main() calls the library function strlwr(s) by passing the string as an argument.
2) The library function strlwr() which converts upper case string into the lowercase string.The strlwr() function is available at ‘string.h’ header file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> #include <string.h> int main() { char s[1000]; printf("Enter the string: "); gets(s); strlwr(s); printf("string in lowercase ='%s'\n",s); return 0; } |
1 2 |
Enter the string: WELCOME string in lowercase ='welcome' |