MartinPaunov

StringToLowerAndUpperExample

May 6th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main(){
  5.  
  6.  
  7.     std::string text = "The QuiCk BroWN FOX"; /// Some string
  8.  
  9.     for(int i = 0; i < text.size(); i++){ /// for loop that takes string.size() and for each iteration
  10.         text[i] = tolower(text[i]);       /// will get the current letter at index i inside the string text
  11.      }                                    /// and replace it with the same letter in lowercase
  12.  
  13.     std::cout << text << std::endl;     ///print in lowercase
  14.  
  15.     for (char& letter : text){          /// The same can be done with foreach loop (since C++11)
  16.         letter = toupper(letter);       /// in this example we are changing the text to uppercase
  17.     }                                   /// char& letter -> is used in order to take the letter by refrence
  18.                                         /// which means by address in the memory so it will be
  19.                                         /// changed if we don't take it like that we will change a copy
  20.                                        
  21.     std::cout << text << std::endl;     /// Prints the text in uppercase
  22.    
  23.     /// There are many more ways to do that check on google.com!
  24.  
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment