Example:
Input string: I love C programming!
Output: i LOVE c PROGRAMMING!
Required knowledge
Basic C programming, Loop, String, FunctionProgram to toggle case of each character
/** * C program to toggle case of each character in a string */ #include <stdio.h> #define MAX_SIZE 100 //Maximum size of the string /* Function declaration */ void toggleCase(char * string); int main() { char string[MAX_SIZE]; int i = 0; /* Reads a string from user */ printf("Enter any string: "); gets(string); printf("String before toggling case: %s\n", string); toggleCase(string); printf("String after toggling case: %s\n", string); return 0; } /** * Swaps the case of each character in given string */ void toggleCase(char * string) { int i = 0; while(string[i] != '\0') { if(string[i]>='a' && string[i]<='z') { string[i] = string[i] - 32; } else if(string[i]>='A' && string[i]<='Z') { string[i] = string[i] + 32; } i++; } }
Output
Enter any string: I love C programming!
String before toggling case: I love C programming!
String after toggling case: i LOVE c PROGRAMMING!
String before toggling case: I love C programming!
String after toggling case: i LOVE c PROGRAMMING!
Happy coding ;)
You may also like
- String programming exercises index.
- C program to convert lowercase string to uppercase.
- C program to convert uppercase string to lowercase.
- C program to count number of alphabets, digits and special characters in a string.
- C program to count number of vowels and consonants in a string.
- C program to count number of words in a string.
- C program to find reverse of a string.
- C program to check whether a string is palindrome or not.
- C program to search all occurrences of a character in a string.
- C program to search all occurrences of a word in a string.