C Program to Change Case of a String

string case conversion

C programs to change the case of a string have been shown here. A string can be converted into various cases like uppercase, lowercase, sentence case, capitalized case, toggle case, alternating case etc.






1. C Program & output to convert a string to uppercase using ASCII value manipulation

Code has been copied
/******************************************
	    	alphabetacoder.com
C Program to convert a string to uppercase 
using ASCII value manipulation 
*******************************************/

#include<stdio.h>

int main() {
    // delclare variables
    char str[100];
    int i = 0;

    // take input of string
    printf("Enter a string: ");
    gets(str);

    // check each charcater in the string str. change 
    // each lowercase letter to its uppecase equivalent.
    // Make no change for other characters.
    while (str[i] != '\0') {
        // check if character is in lowercase
        // If yes, subtract 32 from its ASCII 
        if (str[i] >= 'a' && str[i] <= 'z') {
            str[i] = str[i] - 32;
        }
        i++;
    }
    printf("The string in uppercase: %s\n", str);

    return 0;
}

Output


Enter a string: a qUick brown foX

The string in uppercase: A QUICK BROWN FOX





2. C Program & output to convert a string to uppercase using strupr()

Code has been copied
/****************************
	alphabetacoder.com
C Program to convert a string 
to uppercase using strupr() 
******************************/

#include<stdio.h>
#include<string.h>

int main() {
    // delclare variables
    char str[100];

    // take input of string
    printf("Enter a string: ");
    gets(str);

    // convert and display the string str
    // to uppercase using the function strupr()
    printf("The string in uppercase: %s\n", strupr(str));

    return 0;
}

Output


Enter a string: a qUick brown foX

The string in uppercase: A QUICK BROWN FOX





3. C Program & output to convert a string to lowercase using ASCII value manipulation

Code has been copied
/******************************************
	    alphabetacoder.com
C Program to convert a string to lowercase 
using ASCII value manipulation 
*******************************************/

#include<stdio.h>

int main() {
    // delclare variables
    char str[100];
    int i = 0;

    // take input of string
    printf("Enter a string: ");
    gets(str);

    // check each charcater in the string str. change 
    // each lowercase letter to its uppecase equivalent.
    // Make no change for other characters.
    while (str[i] != '\0') {
        // check if character is in lowercase
        // If yes, subtract 32 from its ASCII 
        if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = str[i] + 32;
        }
        i++;
    }
    printf("The string in lowercase: %s\n", str);

    return 0;
}

Output


Enter a string: A QUICK BROWN FOX

The string in lowercase: a quick brown fox





4. C Program & output to convert a string to lowercase using strlwr()

Code has been copied
/********************************
	alphabetacoder.com
C Program to convert a string to 
lowercase using strlwr() 
*********************************/

#include<stdio.h>
#include<string.h>

int main() {
    // delclare variables
    char str[100];

    // take input of string
    printf("Enter a string: ");
    gets(str);

    // convert and display the string str
    // to lowercase using the function strlwr()
    printf("The string in lowercase: %s\n", strlwr(str));

    return 0;
}

Output


Enter a string: A QUICK BROWN FOX

The string in lowercase: a quick brown fox





5. C Program & output to convert a string to sentence case

Code has been copied
/*********************************************
	     alphabetacoder.com
C Program to convert a string to sentence case 
**********************************************/

#include<stdio.h>

int main() {
    // delclare variables
    char str[100];
    int i;

    // take input of string
    printf("Enter a string: ");
    gets(str);

    // capitalize the first letter of first word.
    if (str[0] != '\0') {
        if (str[0] >= 'a' && str[0] <= 'z') {
            str[0] = str[0] - 32;
        }
    }

    // increment i to start checking from next character
    i++;

    // Keep the other letters in lower-case
    while (str[i] != '\0') {
        // check if characters is in uppercase
        // If yes, add 32 from its ASCII
        if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = str[i] + 32;
        }
        i++;
    }
    printf("The string in the sentence case: %s\n", str);

    return 0;
}

Output


Enter a string: how are you?

The string in the sentence case: How are you?





6. C Program & output to convert a string to capitalized case

Code has been copied
/*************************************************
	    	alphabetacoder.com
C Program to convert a string to capitalized case 
***************************************************/

#include<stdio.h>

int main() {
    // delclare variables
    char str[100];
    int i;

    // take input of string
    printf("Enter a string: ");
    gets(str);

    // capitalize the first letter of first word.
    if (str[0] != '\0') {
        if (str[0] >= 'a' && str[0] <= 'z') {
            str[0] = str[0] - 32;
        }
    }

    // increment i to start checking from next character
    i++;

    // capitalize the first letter of each word
    while (str[i] != '\0') {
        // check if previous charcater is a space.
        // It indicates the starting of new word
        if (str[i - 1] == ' ') {
            // check whether the current charcater is in lowercase
            // If yes, then make it uppercase
            if (str[i] >= 'a' && str[i] <= 'z') {
                str[i] = str[i] - 32;
            }
        }
        // convert other uppercase letters to lowercase
        else if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = str[i] + 32;
        }
        i++;
    }
    printf("The string in the capitalized case: %s\n", str);

    return 0;
}

Output


Enter a string: it is c programming.

The string in the capitalized case: It Is C Programming.





7. C Program & output to convert a string to toggle case

Code has been copied
/*******************************************
	    alphabetacoder.com
C Program to convert a string to toggle case 
*********************************************/

#include<stdio.h>

int main() {
    // delclare variables
    char str[100];
    int i;

    // take input of string
    printf("Enter a string: ");
    gets(str);

    // convert each uppercase letter to lowercase
    // and convert each lowercase letter to uppercase
    while (str[i] != '\0') {
        // check if character is in uppercase
        // If yes, add 32 from its ASCII
        if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = str[i] + 32;
        }
        // check if character is in lowercase
        // If yes, subtract 32 from its ASCII
        else if (str[i] >= 'a' && str[i] <= 'z') {
            str[i] = str[i] - 32;
        }
        i++;
    }
    printf("The string in the toggle case: %s\n", str);

    return 0;
}

Output


Enter a string: Hello World

The string in the toggle case: hELLO wORLD





8. C Program & output to convert a string to alternating case

Code has been copied
/*************************************************
	    	alphabetacoder.com
C Program to convert a string to alternating case 
***************************************************/

#include<stdio.h>

int main() {
    // delclare variables
    char str[100];
    int i;

    // take input of string
    printf("Enter a string: ");
    gets(str);

    // convert each word to alternating case
    // start with lowercase then uppercase, and so on
    while (str[i] != '\0') {
        // odd position charcater should be in lowercase
        if (i % 2 == 0) {
            if (str[i] >= 'A' && str[i] <= 'Z') {
                str[i] = str[i] + 32;
            }
        }
        // even position character should be in uppercase
        else {
            if (str[i] >= 'a' && str[i] <= 'z') {
                str[i] = str[i] - 32;
            }
        }
        i++;
    }
    printf("The string in the alternating case: %s\n", str);

    return 0;
}

Output


Enter a string: a quick brown fox jumps over a lazy dog

The string in the alternating case: a qUiCk bRoWn fOx jUmPs oVeR A LaZy dOg