C Program to Convert Lowercase String to Uppercase

  • Write a C program to convert all lowercase alphabets to uppercase alphabets in a string.

We first take a string as input from user. We have to convert all lowercase alphabets in a input string to uppercase alphabets.
For Example
Input String: Apple
Output String: APPLE

C program to convert lowercase to uppercase using strupr function

This program first takes a string as input from user using gets function and stores it in a character array 'inputString'. It calls strupr function by passing inputString and prints it's response on screen.
Function strupr is used to convert all characters of a string to uppercase. Here is the declaration for strupr function. Function strupr is defined inside string.h header file.

char *strupr(char *string);

/*
* C Program to change lowercase characters 
* to uppercase characters in a string using strupr function
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>

int main(){
    char inputString[100];
    int index;
    printf("Enter a String\n");
    gets(inputString);    
    printf("String with all uppercase characters \n%s ", 
        strupr(inputString));

    getch();
    return 0;
}
Program Output
Enter a String
CProgramming
String with all uppercase characters
CPROGRAMMING

C program to convert lowercase to uppercase using islower and toupper function

Function islower check whether the passed character is lowercase letter or not. It returns a non zero value(true) for lowercase otherwise zero(false). Function toupper converts lowercase alphabets to uppercase. If passed character(c) is lowercase, it returns uppercase equivalent to c, otherwise c remain unchanged. islower and toupper functions are defined in ctype.h header file. Here is the declaration for islower() and toupper() function.

int islower(int c);
int toupper(int c);
/*
* C Program to change lower case characters 
* to upper case characters in a string using toupper function
*/
#include <stdio.h>
#include <conio.h>
#include <ctype.h>

int main(){
    char inputString[100];
    int index;
    printf("Enter a String\n");
    gets(inputString);
    for(index=0; inputString[index] != '\0'; index++){
        /* Check if character in inputArray is lower Case*/
        if(islower(inputString[index])){
            /* Convert lower case character to upper case 
              using toupper function */
            inputString[index] = toupper(inputString[index]);
        } else {
            inputString[index] = inputString[index];
        }
    }
    inputString[index] = '\0';
    printf("String with all uppercase characters \n%s ", inputString);

    getch();
    return 0;
}
Program Output
Enter a String
TechCrashCourse
String with all uppercase characters 
TECHCRASHCOURSE

C program to convert lowercase string to uppercase string using user defined function

Here we are using two user defined functions, isLowerCase() and toUpperCase(). isLowerCase function returns one if passed character is lowercase character otherwise zero. toUpperCase returns uppercase character corresponding to lowerCase character c.

/*
* C Program to change lower case characters 
* to upper case characters in a string using function
*/
#include <stdio.h>
#include <conio.h>

int isLowerCase(char c);
int toUpperCase(char c);

int main(){
    char inputString[100];
    int index;
    printf("Enter a String\n");
    gets(inputString);
    for(index=0; inputString[index] != '\0'; index++){
        /* Check if character in inputArray is lower Case*/
        if(isLowerCase(inputString[index])){
            /* Convert lower case character to upper case*/
            inputString[index] = toUpperCase(inputString[index]);
        } else {
            inputString[index] = inputString[index];
        }
    }
    inputString[index] = '\0';
    printf("String with all upper case characters \n%s",
         inputString);

    getch();
    return 0;
}
/*
 * Returns one if passed character is 
 * lower case character otherwise zero
 */
int isLowerCase(char c){
    if(c >= 'a' && c <= 'z')
        return 1;
    else 
        return 0;
}

/*
 * Returns upper case character corresponding to lowerCase
 * character c
 */
int toUpperCase(char c){
    /* Subtract the difference between lowerCase character 
     * and it's upperCase character(for example:'a'-'A'=32) 
     */
    return c - 32; 
}
Program Output
Enter a String
TECHCrashCourse
String with all uppercase characters 
TECHCRASHCOURSE


Related Topics
C program to convert uppercase string to lowercase
C Program to find frequency of characters in a string
C program to find a substring from a given string
C program to remove vowels from a string
C program to compare two strings
C program to check if two strings are anagram
C program to sort characters of a string
List of all C programs