Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

c++ - Convert Text with spaces to Camel Case

i want to take a film name from user and change that to camel case , my code work if there is no numbers or spaces between letters

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int Count,Length=0;
    string Films;
    
    cout<<"Enter Film Count: ";
    cin>>Count;
    
    for(int i=0;i<Count;i++)
    {
        cout<<"Enter Film Names: ";
        cin>>Films;
        Length=0;
        while(Length<1000)
        {
            switch(Length)
            {
                case 0:       Films[Length]=toupper(Films[Length]); break;
                default:       Films[Length]=tolower(Films[Length]); break;
            }
            Length++;
        }
             cout<<"Results: "<<Films<<endl;
     }

    return 0;
}

i tried other topic solutions but i cant do it correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Problem:

You've chosen the wrong approach to solve the problem. Your current code only changes the first character to uppercase and the rest to lowercase.

Solution:

Instead of using a while and a switch, use a for loop and an if statement that checks for spaces, delete them and change the following characters to uppercase.

Additional information:

  1. using namespace std; is considered a bad practice (More info here).
  2. The while loop can be replaced for a for loop to limit the Length scope and improve readability.
  3. It's a good practice to check whether the std::cin inputs are valid or not to prevent Undefined Behavior.

Full code:

#include <iostream>
#include <string>

int main()
{
    int count;

    std::cout << "Enter film count: ";
    std::cin >> count;
    if(std::cin.fail())
    {
        std::cout << "Invalid input." << std::endl;
        exit(0);
    }
    std::cin.ignore(10000,'
');
    for(int i = 0; i < count; i++)
    {
        std::string film;
        std::cout << "Enter film name: ";
        std::getline(std::cin, film);
        if(std::cin.fail())
        {
            std::cout << "Invalid input." << std::endl;
            exit(0);
        }
        if(film.size() == 0)
            break;
        film[0] = tolower(film[0]);
        for(unsigned int i = 1; i < film.size() - 1; i++)
        {
            if(film[i] == ' ')
            {
                film.erase(i,1);
                film[i] = toupper(film[i]);
                i--;
            }
        }
        std::cout << "Result: " << film << std::endl;
     }

    return 0;
}

Example:

Enter film count: 1
Enter file name: Love live! the school idol movie
Result: loveLive!TheSchoolIdolMovie

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...