Write a C program to convert string from lowercase to uppercase string using loop. How to convert string from lowercase to uppercase using for loop in C programming. C program to convert lowercase to uppercase string using strupr() string function.
Example
Input
Input string: I love Codeforwin.
Output
I LOVE CODEFORWIN.
Required knowledge
Basic C programming, Loop, String
Must know – Program to find length of string
Logic to convert lowercase string to uppercase
Internally in C every characters are represented as an integer value known as ASCII value. Where A is represented with 65 similarly, B with 66.
Below is the step by step descriptive logic to convert string to uppercase.
- Input string from user, store it in some variable say str.
- Run a loop from 0 till end of string.
- Inside loop check if current string is lowercase then convert it to uppercase str[i] = str[i] – 32. Now, why subtracting it with 32. Because difference of a – A = 32.
Algorithm to convert lowercase to uppercase %%Input : text {Array of characters / String} N {Size of the String} Begin: For i ← 0 to N do If (text[i] >= 'a' and text[i] <= 'z') then text[i] ← text[i] - 32; End if End for End
Program to convert string to uppercase
You can trim the above code using pointers. Below is an approach to convert string to uppercase using pointers.
Program to convert string to uppercase using pointers
That was really complex to understand and use. It uses the same first approach to convert string to uppercase. It just uses conditional operators and pointers, in place of if else and array. However, in real life programming it is recommended to use inbuilt library function strupr() defined in string.h to convert to uppercase string.
Read more – Program to string to lowercase
Program to convert string to uppercase using strupr()
Output
Enter your text: I love Codeforwin. Uppercase string : I LOVE CODEFORWIN.
Happy coding
Recommended posts
- String programming exercises index.
- C program to compare two strings.
- C program to copy one string to another string.
- C program to concatenate two strings in one string.
- C program to find reverse of a string.
- C program to check whether string is palindrome or not.
- C program to find total number of vowels and consonants in a string.