C program to toggle each character in a string

Here, we are implementing a C program that will read a string and toggle each character in it, if any character is in uppercase it will be in lowercase and if any character is in lowercase, it will be converted in uppercase.

Example:

Input string: "Hello"
Output: "hELLO"

Program:

/*C program to toggle each character in a string.*/

#include <stdio.h>

int main()
{
    char str[100];
    int counter;

    printf("Enter a string: ");
    gets(str);

    // toggle each string characters
    for (counter = 0; str[counter] != NULL; counter++) {
        if (str[counter] >= 'A' && str[counter] <= 'Z')
            str[counter] = str[counter] + 32; //convert into lower case
        else if (str[counter] >= 'a' && str[counter] <= 'z')
            str[counter] = str[counter] - 32; //convert into upper case
    }

    printf("String after toggle each characters: %s", str);
 
    return 0;
}

Output

Enter a string: This is a Test String.
String after toggle each characters: tHIS IS A tEST sTRING.

C Strings User-defined Functions Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.