English 中文(简体)
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 57 MINUTES 17 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
原标题:Breaking a string into words at each capital letter [closed]

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 57 MINUTES 14 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

Closed 10 mins ago.

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 57 MINUTES 13 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

#include <iostream>
#include <vector>
#include <string>

std::string capSpace(std::string txt) {
    std::vector <char> un_cap;
    std::vector <char> caps = { A , B , C , D , E , F , G , H , I , J , K , L , M , N , O , P , Q , R , S , T , U , V , W , X , Y , Z };
    std::vector <char> small = { a , b , c , d , e , f , g , h , i , j , k , l , m , n , o , p , q , r , s , t , u , v , w , x , y , z };
    for (int i=1; i<=txt.size(); i++) {
        un_cap.push_back(txt[i-1]);
    }
    for (int j=1; j<=un_cap.size(); j++) {
        for (int k=1; k<= caps.size(); k++) {
            if (caps[k-1] == un_cap[j-1]) {
                un_cap[j-1] == small[k-1];
                un_cap.push_back(   );
                for (int l= un_cap.size(); l>= j+1; l--) {
                    un_cap[l-1] == un_cap[l-2];
                }
                un_cap[j-1] =    ;
                k = caps.size();
            }
        }
    }
    
    for (int m=1; m<=un_cap.size(); m++) {
        txt[m-1] = un_cap[m-1];
    }
    return txt;
}

int main() {
std::string text;
std::cout << "Enter a text to uncap and space ";
std::cin >> text;
capSpace(text);
std::cout << "The uncapped version is " << text << "
";
}

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 57 MINUTES 11 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

问题回答

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 57 MINUTES 10 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 57 MINUTES 08 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

#include <iostream>
#include <vector>
#include <string>

void capSpace(std::string &txt) {
    std::string un_cap;
    un_cap.reserve(txt.size()); //make some space for our text and avoid too much reallocations

    for (char ltr : txt) { //iterate over txt
        if (isupper(ltr)) {
            un_cap +=    ;
        }
        un_cap += tolower(ltr);
    }

    txt = un_cap;
}

int main() {
    std::string text;
    std::cout << "Enter a text to uncap and space ";
    std::cin >> text;
    capSpace(text);
    std::cout << "The uncapped version is " << text << "
";
}

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 57 MINUTES 07 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

#include <iostream>
#include <vector>
#include <string>

std::string capSpace(const std::string &txt) {
    std::string un_cap;
    un_cap.reserve(txt.size()); //make some space for our text and avoid too much reallocations

    for (char ltr : txt) { //iterate over txt
        if (isupper(ltr)) {
            un_cap +=    ;
        }
        un_cap += tolower(ltr);
    }

    return un_cap;
}

int main() {
    std::string text;
    std::cout << "Enter a text to uncap and space ";
    std::cin >> text;
    std::cout << "The uncapped version is " << capSpace(text) << "
";
}

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 57 MINUTES 05 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

"The uncapped version is it is football not basketball"

EXPLANATION: notice how there is an & before the parameter txt, this makes it so that the function doesn t make a copy of the original value and actually modifies it, but it the case of solution 2, it s a const, wich makes points out that the original value is not being modified, and instead, the function is returning an std::string.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签