Click here to Skip to main content
14,667,814 members
Rate this:
Please Sign up or sign in to vote.
See more:
What the class describes is about "reversing a string", which is correct and usable from the Leetcode website. Today, I want to present "reversing a string" by inputting a value by myself (such as the int main() part below), but I still can't execute it after thinking for a long time. Beginners sincerely ask for advice, maybe you can also attach your writing so that I can learn, thank you.

#include <iostream>
#include <string>
using namespace std;
    
class Solution
{
    public:
    string reverseWords(string s)
    {
        if (s.size() == 0)
        { 
            return s;
        }
        int front = 0, back = 0; 
        for (int i = 0; i < s.size() - 1; i++)
        {
            if (s[i] != ' ')
            {
                back++;
            }
            else
            {
                reverse(s.begin() + front, s.begin() + back); 
                front = back + 1;                             
                back = front;                                
            }
        } 
        back++;
        reverse(s.begin() + front, s.begin() + back); 
        return s;
    }
};
    
int main()
{
    Solution word01;
    string s1= "Hello caterpillar";
    word01 s1;
    cout << s1.reverseWords();
}


What I have tried:

I want to present "reversing a string" by inputting a value by myself (such as the int main() part below), but I still can't execute it after thinking for a long time.
Posted
Updated 30-Aug-20 22:55pm
v3
Comments
Sandeep Mewara 31-Aug-20 4:55am
   
And what is the issue or where are you stuck?

BTW, you trying to do string reversal or work reversal? An example to your query would help.
Sandeep Mewara 31-Aug-20 4:55am
   
And what is the issue or where are you stuck?

BTW, you trying to do string reversal or work reversal? An example to your query would help.
Amy Zhou 31-Aug-20 6:49am
   
1. My question is to send the string that I want back to class.
2. String reversal.

The problem has been resolved, thank you very much for your answer!

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Look at your code:
Solution word01;
string s1= "Hello caterpillar";
word01 s1;
cout << s1.reverseWords();
What do you expect that to do?
Solution word01;
Declares a variable called word01 of type Solution
string s1= "Hello caterpillar";
Declares a variable called s1 of type string and assigns it a value - a string literal.
word01 s1;
Puts two variables side by side without doing anything with them ...
That last bit won't compile: the system has no idea what to do with it!
cout << s1.reverseWords();
Tries to call a method in the string class called reverseWords on the string in the variable s1. Since there is no method called reverseWords ion teh string class, that also won't compile.

reverseWords is part of the Solution class, you need to use your instance of the class, call a method on that, and pass your string to it as a parameter:
Solution word01;
string s1= "Hello caterpillar";
cout << word01.reverseWords(s1);
   
Comments
Amy Zhou 31-Aug-20 6:16am
   
I got it, thanks for your help!
OriginalGriff 31-Aug-20 6:29am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100