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.
/*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.
Program:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer