Try to search your question here, if you can't find : Ask Any Question Now ?

How to captalize and tittle a string

HomeCategory: stackoverflowHow to captalize and tittle a string
Avatarwillson asked 4 months ago

If I was given a string, I need to either captalize or title the string. for example:

   captalize("hello world")   output = "Hello world"
   captalize("HELLO WORLD")   output = "Hello world"
   captalize(" HELLO WORLD")  output = " hello world"
   Tittle("hello world")      output = "Hello World"
   Tittle("HELLO WORLD")      output = "Hello World"
   Tittle(" HELLO WORLD")     output = "Hello World"

here is what I have done so far:

 std::string Capitalize(const std::string &str) {

    std::string Ret;
    for (int i = 0; i < Ret.length(); i++){
        char c = str[i];
        if (i == 0){
            Ret += toupper(c);
        }
        else if (i != 0){
            Ret += (tolower(c));
        }
    }
    return Ret;
}

std::string Title(const std::string &str) {
    // Your code goes here
    std::string Ret;
    for (auto &c:str) {
        Ret += tolower(c);
    }

    int size = Ret.length();
    for (int i = 0; i < size; i++) {
        if (Ret[i - 1] == ' ') {
            Ret[i] = toupper(Ret[i]);
        }
    }
    return Ret;

}

For some reason its not working and I need a way to fix this.

1 Answers
Best Answer
AvatarFernando answered 4 months ago
Your Answer

6 + 0 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com