
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convert characters of a string to opposite case in C++
We are given a string of any length and the task is to convert the string having uppercase letters to lowercase letters and lowercase letters to uppercase letters.
For Example
Input − string str = ”Welcome To The Site!”
Output − wELCOME tO tHE sITE!
Explanation − converted the letters W, T, T, S to lowercase and letters e,l,c,o,m,e,o,,i,t,e to uppercase and it doesn’t perform any operations to the special characters.
Input − string str = ”HELLO”
Output − hello
Explanation − converted the letters H,E,L,L,E to lowercase.
This can be done using two different approaches
Using inbuilt functions provided by C++ to perform these operations and those are toLowerCase(char) and toUpperCase(char).
Through the logic, which we are implementing in the below program.
Approach used in the below program is as follows
Input the string of any length
Calculate the length of the string using the length() function that will return an integer value as per the number of letters in the string including the spaces.
The ASCII values of uppercase letters[A-Z] start with 65 till 90 and lowercase letters[a-z] starts with 97 till 122.
Start the loop which will compare each letter in a string. If a letter is in uppercase then add 32 to convert it to lowercase and if the letter is in lowercase then subtract 32 to convert it to uppercase.
Print the string.
Example
#include<iostream> using namespace std; void Convert_case(string &str){ //calculate the length of a string int len = str.length(); //converting lowercase to uppercase and vice versa for (int i=0; i<len; i++){ 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; } } } int main(){ string str = "What’s Your Name?"; cout<<"String before conversion is: "<<str; Convert_case(str); cout<<"\nString after conversion is: "<<str; return 0; }
Output
If we run the above code it will generate the following output −
String before conversion is − What’s Your Name? String after conversion is &mius; wHAT’S yOUR nAME?
- Related Articles
- Convert a String to a List of Characters in Java
- Convert mixed case string to lower case in JavaScript
- Convert a C++ String to Upper Case
- Convert List of Characters to String in Java
- How to convert a string to camel case in JavaScript?
- Program for converting Alternate characters of a string to Upper Case.\n
- How to convert a list of characters into a string in C#?
- Java program to convert a list of characters into a string
- C# program to convert a list of characters into a string
- How to convert a string into Title Case in Golang?
- How to convert an array of characters into a string in C#?
- How to convert string to title case in C#?
- Java program to count upper and lower case characters in a given string
- C# program to count upper and lower case characters in a given string
- How to convert a string vector into title case in R?
