Toggle the case of each character of a string with C.
#include <stdio.h>
/* Toggle case function declaration */
void toggleCase(char *str);
int main()
{
char str[100];
/* Input string from user */
printf("Enter any string: ");
gets(str);
printf("String before toggling case: %s", str);
toggleCase(str);
printf("String after toggling case: %s", str);
return 0;
}
/**
* Toggle case of each character in given string
*/
void toggleCase(char *str)
{
int i = 0;
while (str[i] != '\0')
{
if (str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}
else if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + 32;
}
i++;
}
}
Output
Enter any string : rezaul karim
String before toggling case:
rezaul karim
String after toggling case:
REZAUL KARIM
0 Comments