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.

40,024 questions

51,976 answers

573 users

How to convert a string to PascalCase using RegEx in C++

1 Answer

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

std::string getPascalCase(const std::string& input) {
    std::string str = input;
    if (str.find(" ") == std::string::npos) {
        str = std::regex_replace(str, std::regex("([a-z])([A-Z])"), "$1 $2"); 
    }

    std::string result;
    bool capitalizeNext = true;
    for (char ch : str) {
        if (ch == ' ' || ch == '_') {
            capitalizeNext = true;
        } else if (capitalizeNext) {
            result += static_cast<char>(std::toupper(ch));
            capitalizeNext = false;
        } else {
            result += static_cast<char>(std::tolower(ch));
        }
    }
    return result;
}

int main() {
    std::cout << getPascalCase("get file content") << std::endl;
    std::cout << getPascalCase("get_file_content") << std::endl;
    std::cout << getPascalCase("get______file___content") << std::endl;
    std::cout << getPascalCase("get______file____  content") << std::endl;
    std::cout << getPascalCase("GET FILE CONTENT") << std::endl;
    std::cout << getPascalCase("get    file      content") << std::endl;
    std::cout << getPascalCase("getFileContent") << std::endl;
}


  
/*
run:
  
GetFileContent
GetFileContent
GetFileContent
GetFileContent
GetFileContent
GetFileContent
GetFileContent
  
*/

 



answered Feb 22, 2025 by avibootz

Related questions

1 answer 86 views
2 answers 99 views
1 answer 81 views
1 answer 89 views
1 answer 86 views
1 answer 77 views
...