ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.