Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++
  • Standard Template Library
  • STL Vector
  • STL List
  • STL Set
  • STL Map
  • STL Stack
  • STL Queue
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators
Open In App
Next Article:
How to Convert a std::string to char* in C++?
Next article icon

How to Convert std::string to Lower Case?

Last Updated : 04 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Converting string to lower case means all the alphabets present in the string should be in lower case. In this article, we will learn how to convert the std::string to lowercase in C++.

Examples

Input: s = "GEEKS for GEEKS"
Output: geeks for geeks
Explanation: All characters of s are converted to lowercase.

Input: s = "HelLO WoRld"
Output: hello world
Explanation: All characters of s are converted to lowercase.

Following are the 3 different methods to convert the std::string (C++ Style Strings) to lower case in C++:

Table of Content

  • Using std::transform()
  • Using std::for_each()
  • Manually Using ASCII Values

Using std::transform()

We can convert the given std::string to lower case using std::transform() with std::tolower() method. This method applies the given tolower() function to each of the character converting it to lowercase if its not.

Syntax

std::transform(s.begin(), s.end(), s.begin(), ::tolower);

where, s is the string to be converted.

Example

C++
// C++ Program to convert the std::string to // lower case using std::transform() #include <bits/stdc++.h> using namespace std;  int main() {     string s = "GEEKS for GEEKS";      // Converting the std::string to lower case     // using std::transform()     transform(s.begin(), s.end(), s.begin(),               ::tolower);      cout << s;     return 0; } 

Output
geeks for geeks

Time Complexity: O(n), where n is the length of string.
Auxiliary Space: O(1)

Using std::for_each()

We can also convert the given std::string to lower case using std::for_each loop with lambda function that convert the given character to its lower case using tolower() function.

Example

C++
// C++ Program to convert the std::string to // lower case using std::for_each loop with // lambda expression #include <bits/stdc++.h> using namespace std;  int main() {     string s = "GEEKS for GEEKS";      // Converting the std::string to lower case     for_each(s.begin(), s.end(), [](char& c) {         c = tolower(c);     });      cout << s;     return 0; } 

Output
geeks for geeks

Time Complexity: O(n), where n is the length of string.
Auxiliary Space: O(1)

Manually Using ASCII Values

C++ uses ASCII charset in which characters are represented by 7-bit numbers called ASCII numbers. Also, lowercase alphabet for corresponding uppercase alphabet always differs by 32. So, we can use this property to convert each character of std::string to lowercase one by one by adding 32 to the uppercase character.

Example

C++
// C++ program to convert std::string to lowercase // manually using ASCII values #include <bits/stdc++.h> using namespace std;  void toLowerCase(string& s) {      	// Manual converting each character to lowercase   	// using ASCII values     for (char &c : s) {         if (c >= 'A' && c <= 'Z') {                      	// Convert uppercase to lowercase           	// by adding 32             c += 32;         }     } }     int main() {     string s = "GEEKS for GEEKS";      	// Converting s to lowercase   	toLowerCase(s);      cout << s;     return 0; } 

Output
geeks for geeks

Time Complexity: O(n), where n is the length of string.
Auxiliary Space: O(1)


Next Article
How to Convert a std::string to char* in C++?

B

bhushanc2003
Improve
Article Tags :
  • C++ Programs
  • C++
  • STL
  • cpp-string
  • CPP Examples
Practice Tags :
  • CPP
  • STL

Similar Reads

  • How to Convert a std::string to char* in C++?
    In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor
    1 min read
  • Convert char* to std::string in C++
    Strings are generally represented as an instance of std::string class in C++. But the language also supports the older C-Style representation where they are represented as array of characters (char* or char[]) terminated by null character '\0'. In this article, we will learn how to convert the char*
    3 min read
  • How to Convert a Single Character to String in C++?
    A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++. Example: Input: c = 'A'Output: s = "a"Explanation: Character c is converted to a string. Input: c = '#'Output: s = "#"Explanation: Character c is
    4 min read
  • Convert Float to String In C++
    In this article, we learn how we can convert float to string in C++ using different methods: Using the to_string()Using stringstreamUsing MacrosUsing lexical_cast from the boost library1. Using to_string() The to_string() method takes a single integer variable or other data type and converts it into
    3 min read
  • How to Convert a C++ String to Uppercase?
    Converting a string to uppercase means changing each character of the string that is in lowercase to an uppercase character. In the article, we will discuss how to convert a string to uppercase in C++. Examples Input: str = "Geeksforgeeks"Output: GEEKSFORGEEKSExplanation: Every character of string c
    3 min read
  • Convert String to Integer Vector in C++
    Strings are sometimes used to represent the numerical data which needs to be converted back to integer, but sometimes we may need to convert it to vector of integers instead due to some problems such as too large integer. In this article, we will learn different way to convert a string to integer ve
    2 min read
  • Convert Vector of Characters to String in C++
    In this article, we will learn different methods to convert the vector of character to string in C++. The most efficient method to convert the vector of characters to string is by using string's range constructor. Let’s take a look at an example: [GFGTABS] C++ #include <bits/stdc++.h> using na
    2 min read
  • How to Convert wstring to int in C++?
    In C++, std::wstring is a type of string where each character is of a wide character type. These types of strings can be used to store the numerical strings which can then be converted to their corresponding type. In this article, we will see how to convert a wstring to an int in C++. Input: wstr =
    2 min read
  • How to Reverse a String in C++?
    Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++. Examples Input: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced
    2 min read
  • How to Convert wstring to double in C++
    In C++, std::wstring is a type of string where each character is of a wide character type. Converting wstring to a double in C++ can be a common requirement especially when dealing with the inputs that are stored as unicode characters. In this article, we will learn how to convert a wstring to a dou
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences