How to extract uppercase characters (capital letters) from a string in C++

2 Answers

0 votes
#include <iostream> 
#include <string>

std::string extract_uppercase_letters(std::string s) { 
    int len = s.length();
       
    std::string uppercaseChars = "";
     
    for (int i = 0; i < len; i++) { 
        if (s[i] >= 65 && s[i] <= 90) {
            uppercaseChars += s[i]; 
        }
    } 
     
    return uppercaseChars;
} 
     
int main() 
{                       
    std::string s = "cpCPppPrPoRO"; 
     
    std::string ucl = extract_uppercase_letters(s);
      
    std::cout << ucl;
} 
   
   
   
/*
run:
   
CPPPRO
   
*/

  

 



answered Nov 1, 2019 by avibootz
edited Nov 1, 2024 by avibootz
0 votes
#include <iostream> 
#include <string>
#include <cctype>

std::string extract_uppercase_letters(std::string s)  { 
    int len = s.length();
      
    std::string uppercaseChars = "";
    
    for (char ch : s) {
        if (isupper(ch)) {
            uppercaseChars += ch;
        }
    }
    
    return uppercaseChars;
} 
    
int main() 
{                       
    std::string s = "cpCPppPrPoRO"; 
    
    std::string ucl = extract_uppercase_letters(s);
     
    std::cout << ucl;
}
  
  
  
/*
run:
  
CPPPRO
  
*/

 



answered Nov 1, 2024 by avibootz
...