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.

Write a C program to convert lowercase string to uppercase string using inbuilt string library function strupr(). How to convert lowercase string to uppercase using library function. How to use strupr() string library function in C programming.

Example:
Input string: I love programming!
Output string: I LOVE PROGRAMMING!

Required knowledge:

Basic C programming, Loop, String

Logic to convert lowercase string to uppercase

Internally characters in C are represented as an integer value known as ASCII value. Which means if we write A or any other character it is translated into a numeric value in our case it is 65 as ASCII value of A = 97.

Here what we need to do is first we need to check whether the given character is lowercase alphabet or not. If it is lowercase alphabet just subtract 32 from it which will result in uppercase alphabet (Since ASCII value of A=65, a=97 their difference is 97-65 = 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 lowercase string to uppercase

/** 
 * C program to convert lowercase string to uppercase without using inbuilt library function
 */

#include <stdio.h>

#define MAX_SIZE 100 //Maximum size of the string


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

    /* Reads a string from user */
    printf("Enter your text : ");
    gets(string);

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

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

Note: You can also use inbuilt library function strupr() to convert to uppercase string. It is defined under string.h header file.

Program to convert lowercase 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 size of the string


int main()
{
    char string[MAX_SIZE];

    /* Reads a string from user */
    printf("Enter your text : ");
    gets(string);

    strupr(string); //Converts to uppercase

    printf("Uppercase string : %s\n", string);

    return 0;
}


Output
Enter your text: I love programming!
Uppercase string : I LOVE PROGRAMMING!


Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment