I'm working on a project and I have to extract each word of a line that the operator just entered in the program. I'm not sure how to do that. Any help with be super appreciated.

Here is my code...

#include<iostream>
#include<string>
using namespace std;!!
class StringModify!
{!
public:!
void get_data();!
//takes a line of input from the user
void extract_word();!
//extracts each word from the input line
string reverse_word(const string& s);!
//returns a string which is reverse of s
void swap(char& v1, char& v2);!
//interchanges value of v1 and v2
void append(const string& reverse_word);!
//puts together each reversed word with whitespaces to get formatted_line
void display();!!
//displays both input line and formatted_line
private:!
string line;! //original string
string formatted_line;!//formatted string
};
int main()
{
StringModify data1;

string get_data;

cout<<"Enter the string: ";
get_data(string);
cout<<"The original string is: "<<get_data<<endl;

return 0;
}
void StringModify::get_data()!
}

}
void StringModify::extract_word()!
{
// Insert your code here
}
string StringModify::reverse_word(const string& s)
{
// Insert your code here
}
void StringModify::swap(char& v1, char& v2)
{
// Insert your code here
}
)

At least make some attempt at the StringModify methods first. Just posting the skeleton provided by your instructor isn't sufficient.

I have been working on my program and I have a few errors. Can anyone help me now that I have figured some stuff out?

#include<iostream>
#include<string>
using namespace std;!!
class StringModify!
{!
public:!
void get_data();!
//takes a line of input from the user
void extract_word();!
//extracts each word from the input line
string reverse_word(const string& s);!
//returns a string which is reverse of s
void swap(char& v1, char& v2);!
//interchanges value of v1 and v2
void append(const string& reverse_word);!
//puts together each reversed word with whitespaces to get formatted_line
void display();!!
//displays both input line and formatted_line
private:!
string line;! //original string
string formatted_line;!//formatted string
};
int main()
{
StringModify data1;

string get_data;

cout<<"Enter the string: ";
get_data(string);
cout<<"The original string is: "<<get_data<<endl;

return 0;
}
void StringModify::get_data()!
}

}
void StringModify::extract_word()!
{

}
string StringModify::reverse_word(const string& s)
{
    int start = 0;
    int end = s.length();
    string temp(s);

    while (start < end)
    {
        end--;
        swap(temp[start], temp [end]);
        start++;
    }

    return temp;
}

void StringModify::swap(char& v1, char& v2)
{
    char temp = v1;
    v1 = v2;
    v2 = temp;
}
)

What errors are you getting. What you have put forth there doesn't compile due to a set of mismatched braces and the stray exclamation points. Also, you're passing "string" to your method on line 30, which is incorrect.

I changed the "string" on line 30 to "string line".

I have 3 errors.

line 39: expected primary expression before ')' token
line 45: expected initializer before '}' token
line 45: expected declaration before '}' token

Try again with line 30, you just need line, there's no need to put the type when evoking a method.

Flip the brace around (i.e., "}" to "{" ) on line 36 See if that straightens out some of the other errors. Otherwise, check over your code carefully, fix any blatant error you can find and repost it.

39 has the stray "!" that needs be gotten rid of

I fixed everything except for the error on line 39. It states the same thing..."expected primary expression before ')' token"

On line 40 I changed get_data() to get_date(string line).

For the get_data all I need to do is get the user to enter a line of input and read it into the private member variable "line" so that's why I used the "string line" from the private class.

And another problem is I have no idea how to extract each word from the line that the user entered.

For the extraction, are you allowed to use vectors?

Also, please repost your current code. Make sure there are no stray "!" in it.

#include<iostream>
#include<string>
using namespace std;
class StringModify
{
public:
void get_data();
//takes a line of input from the user
void extract_word();
//extracts each word from the input line
string reverse_word(const string& s);
//returns a string which is reverse of s
void swap(char& v1, char& v2);
//interchanges value of v1 and v2
void append(const string& reverse_word);
//puts together each reversed word with whitespaces to get formatted_line
void display();
//displays both input line and formatted_line
private:
string line; //original string
string formatted_line;//formatted string

};
int main()
{
StringModify data1;

string get_data;

cout<<"Enter the string: ";
get_data(string line);
cout<<"The original string is: "<<get_data<<endl;

return 0;
}
void StringModify::get_data()
{

}
void StringModify::extract_word()
{

}
string StringModify::reverse_word(const string& s)
{
    int start = 0;
    int end = s.length();
    string temp(s);

    while (start < end)
    {
        end--;
        swap(temp[start], temp [end]);
        start++;
    }

    return temp;
}

void StringModify::swap(char& v1, char& v2)
{
    char temp = v1;
    v1 = v2;
    v2 = temp;
}

As for the extraction...we aren't allowed to use the strtok() function.

Line 31, still incorrect. According to the definition of your get_data function, it doesn't take any parameters, which is correct as you will write into line via your get_data.

For the extraction, think about what it means for something to be a word. There's going to be spaces (which means you will need to use a getline function in your get_data). Easier if you use a vector to do it if you don't know the number of words you are expecting, but if you can't, use an array of strings that has a high dimension.

Not to be critical, but you need to plan some of this out with pencil and paper before trying to code it.

You told me that line 31 is still incorrect but then you said it was correct. So is it correct or incorrect?

The line is going to be "THIS IS PROJECT5 OF EECS138" so how do I do that?

I never said it was correct. I meant that get_data should take no parameters at all, so your method definition is correct, but the call in main() is not correct either way.

So if you know the number of strings, make an array of strings that will hold 5 strings. Start at the beginning of the original string, read until you hit a space and store that word (hint, the += operator may be helpful to you). Do this for each word until the end of the original string.

Read your text to determine which property of std::string you can use for this. Hint: how do you access the individual characters of a string.

The best way to do this is with stringstreams, but that's probably not allowed in your course either. I add this because someone is bound to come along and say "why don't you use stringstreams??"

Ok so I researched and then modified my code so here it is now.

#include<iostream>
using std::cout;
using std::endl;
#include<string>
using std::string;
using namespace std;
class StringModify
{
public:
void get_data();
//takes a line of input from the user
void extract_word();
//extracts each word from the input line
string reverse_word(const string& s);
//returns a string which is reverse of s
void swap(char& v1, char& v2);
//interchanges value of v1 and v2
void append(const string& reverse_word);
//puts together each reversed word with whitespaces to get formatted_line
void display();
//displays both input line and formatted_line
private:
string line; //original string
string formatted_line;//formatted string
};

int main()
{
StringModify data1;

string get_data;

cout<<"Enter the string: ";
getline (string line);
cout << "Str is : " << string line << endl;

return 0;
}
void StringModify::get_data()
{

}
void StringModify::extract_word()
{

}
string StringModify::reverse_word(const string& s)
{
    int start = 0;
    int end = s.length();
    string temp(s);

    while (start < end)
    {
        end--;
        swap(temp[start], temp [end]);
        start++;
    }

    return temp;
}

void StringModify::swap(char& v1, char& v2)
{
    char temp = v1;
    v1 = v2;
    v2 = temp;
}

Ok, but you haven't made any progress on your methods... Take more than a few minutes, think it through, code, test, code, test, and see what you come up with. I'm not going to do this for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.