Click here to Skip to main content
15,883,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

Look at your code:
C++
Solution word01;
string s1= "Hello caterpillar";
word01 s1;
cout << s1.reverseWords();
What do you expect that to do?
C++
Solution word01;
Declares a variable called word01 of type Solution
C++
string s1= "Hello caterpillar";
Declares a variable called s1 of type string and assigns it a value - a string literal.
C++
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!
C++
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:
C++
Solution word01;
string s1= "Hello caterpillar";
cout << word01.reverseWords(s1);
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900