C program to convert lowercase string to uppercase

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.

  1. Input string from user, store it in some variable say str.
  2. Run a loop from 0 till end of string.
  3. 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

/** 
 * C program to convert string to uppercase
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size


int main()
{
    char str[MAX_SIZE];
    int i;

    /* Input string from user */
    printf("Enter your text : ");
    gets(str);

    for(i=0; str[i]!='\0'; i++)
    {
        /*
         * If current character is lowercase alphabet then
         * convert it to uppercase.
         */
        if(str[i]>='a' && str[i]<='z')
        {
            str[i] = str[i] - 32;
        }
    }

    printf("Uppercase string : %s",str);
    return 0;
}

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

/** 
 * C program to convert string to uppercase using pointers
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size


int main()
{
    char str[MAX_SIZE];
    char * s = str;

    /* Input string from user */
    printf("Enter your text : ");
    gets(str);

    while(*s) 
    {
        *s = (*s > 'a' && *s <= 'z') ? *s-32 : *s;
        s++;
    }

    printf("Uppercase string : %s",str);
    return 0;
}

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()

/** 
 * C program to convert lowercase to uppercase string using strupr()
 */

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size 


int main()
{
    char str[MAX_SIZE];

    /* Input string from user */
    printf("Enter your text : ");
    gets(str);

    strupr(str); // Convert to uppercase

    printf("Uppercase string : %s", str);

    return 0;
}

Output

Enter your text: I love Codeforwin.
Uppercase string : I LOVE CODEFORWIN.

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>