Q:

C program to toggle each character in a string

0

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"

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now