C++ program to toggle each characters in a string

Toggling each character in a string.

In this article we will learn how to write a C++ program to toggle each character in a string. Toggling characters means to convert lower case characters to upper case characters and upper case characters to lower case characters. This can be simply achieved by using ASCII values as there is a difference of 32 between lower case and upper case alphabets ASCII values.

C++ program to toggle each characters in a string

Algorithm:

  • Initialize the variables.
  • Accept the input.
  • Initiate a for loop and terminate it at the end of string.
  • Toggle each character using ASCII values.
  • Print toggled string.

C++ programming code to toggle each character in a string.

Competitive Coding Techniques
#include <iostream>
#include <string.h>
using namespace std;
 
int main()
{
    //Initializing variable.
    char str[100];
    int i;
  	
    //Accepting input.
    cout<<"Enter the String: ";
    gets(str);
  	
  	//Initializing for loop.
  	for (i = 0; str[i]!='\0'; i++)
  	{
  	    //Toggling characters.
  	    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; } } cout<<"\nToggoled string: "<<str<<endl; //Printing toggled string. return 0; }
Output:
Enter the String: PREPinsta
Toggoled string: prepINSTA

Operations on Strings

  • Length of the string without using strlen() function : C | C++ | Java | Python
  • Toggle each character in a string : C | C++ | Java | Python
  • Count the number of vowels : C | C++ | Java | Python
  • Remove the vowels from a String : C | C++ | Java | Python
  • Check if the given string is Palindrome or not : C | C++ | Java | Python
  • Print the given string in reverse order : C | C++ | Java | Python
  • Remove all characters from string except alphabets : C | C++ | Java | Python
  • Remove spaces from a string : C | C++ | Java | Python
  • Remove brackets from an algebraic expression : C | C++ | Java | Python
  • Count the sum of numbers in a string : C | C++ | Java | Python
  • Capitalize the first and last character of each word of a string : C | C++ | Java | Python
  • Calculate frequency of characters in a string : C | C++ | Java | Python
  • Find non-repeating characters in a string : C | C++ | JAVA | Python
  • Check if two strings are Anagram or not : C | C++ | Java | Python
  • Replace a sub-string in a string: C | C++ | Java | Python
  • Count common sub-sequence in two strings : C | C++ | Java | Python
  • Check if two strings match where one string contains wildcard characters : C | C++ | Java | Python
Operations on Strings