Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,988 questions

51,933 answers

573 users

How to remove punctuation from a string and lowercase the string in C++

2 Answers

0 votes
#include <iostream>
#include <algorithm>

std::string toLower(std::string str) {
    for_each(str.begin(), str.end(), [](char & ch) {
        ch = tolower(ch);
    });
       
    return str;
}
 
std::string removePunctuationAndLowercaseString(const std::string& str) {
    std::string tmp(str);
    
    tmp.erase(std::remove_if(tmp.begin(), tmp.end(), ispunct), tmp.end());
     
    return toLower(tmp);
}
 
int main() {
    std::string str = "CPP is, a &general (purpose) @PROGRAMMING <language>.";
 
    str = removePunctuationAndLowercaseString(str);
     
    std::cout << str;
}

 
 
/*
run:
 
cpp is a general purpose programming language
 
*/
 

 



answered Jun 22, 2024 by avibootz
0 votes
#include <iostream>
#include <algorithm>

std::string toLower(std::string str) {
    transform(str.begin(), str.end(), str.begin(), ::tolower);
       
    return str;
}
 
std::string removePunctuationAndLowercaseString(const std::string& str) {
    std::string tmp(str);
    
    tmp.erase(std::remove_if(tmp.begin(), tmp.end(), ispunct), tmp.end());
     
    return toLower(tmp);
}
 
int main() {
    std::string str = "CPP is, a &general (purpose) @PROGRAMMING <language>.";
 
    str = removePunctuationAndLowercaseString(str);
     
    std::cout << str;
}

 
 
/*
run:
 
cpp is a general purpose programming language
 
*/
 

 



answered Jun 22, 2024 by avibootz

Related questions

1 answer 86 views
2 answers 179 views
1 answer 91 views
1 answer 86 views
1 answer 81 views
1 answer 78 views
...