This is a C++ Program to Toggle Cases in a String.
The program takes a string and changes the cases of the string characters.
1. The program takes a string.
2. Using ctype functions, uppercase characters are converted to lowercase and vice versa.
3. If entered string is not alphabetical, program is exited.
4. The converted string is printed.
5. Exit.
Here is the source code of C++ Program to Toggle Cases in a String. The program output is shown below.
#include<iostream>
#include<string.h>
#include<ctype.h>
using namespace std;
int main ()
{
char str[50];
cout << "Enter a string : ";
gets(str);
for (int i = 0; str[i] != '\0'; i++)
{
if (isalpha(str[i]))
{
if (islower(str[i]))
str[i] = toupper(str[i]);
else
str[i] = tolower(str[i]);
}
else
{
cout << "Entered string is not alphabetical.";
exit(0);
}
}
cout << "Resultant string : " << str;
}
1. The user is asked to enter a string. It is stored in the character variable ‘str’.
2. Using a for loop, each character in the string is checked.
3. If it is an alphabet, it is further checked if it’s uppercase or lowercase.
4. If the character is uppercase, it is converted to lowercase, else vice versa.
5. If character is not an alphabet, the program is exited.
6. The converted string is then printed.
Case 1 : Enter a string : sPANISH Resultant string : Spanish Case 2 : Enter a string : halo25 Entered string is not alphabetical. Case 3 : Enter a string : EUPHORIA Resultant string : euphoria
Sanfoundry Global Education & Learning Series – C++ Programs.
To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.
- Practice Computer Science MCQs
- Practice Programming MCQs
- Apply for Computer Science Internship
- Check C++ Books
- Apply for C++ Internship