954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String Parsing

Hi i am trying to write a program where the user inputs a list of space separated words (more than ten are required); the program then reverses and prints the order of the words.
Example: input:“Extra Credit Programs”; output: “Programs Credit Extra”
but my program s far is not behaving correctly. can anyone help me please.

Attachments reverse_order.cpp (0.53KB)
hawita
Light Poster
39 posts since Feb 2011
Reputation Points: 7
Solved Threads: 0
 

Please, just post the code inside code blocks, as in

// This is C++ code

Asking us to download data and then analyze it is inconvenient at best, and a vector for malware at worst. :-(

rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179
 

OKEY HERE it is:

#include<iostream.h>
#include<string>
using namespace std;

main() {
     std::cout << "Please enter a sequence of words: ";
     std::string y;  // read words and write them one per line
sort (y.begin(), y.end());   
void reverseArrayElement( string y[], int begin, int end); // reverse the elements of a string array
 while(cin >> y) {
    std::cout << y << endl;}
    


//    begin is the index of the first element in the range to be reversed, and end is the index after the last element in the range

system("Pause");

}
hawita
Light Poster
39 posts since Feb 2011
Reputation Points: 7
Solved Threads: 0
 

You posted Code that makes no sense at all.

1) Output a message
2) define a string variable
3) Sort nothing since all you did was declare a variable
4) declare a prototype for a non-existant function
5) start a loop the 
   a) inputs a string
   b) outputs the string


You need to think through what you want to do and put the steps in their proper order.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

some hints/psuedocode

1) create a std::stack of strings
2) Either read words one by one, ending with a sentential value or read the whole sentence
3) If reading words one by one, just add it to the stack else if reading the whole sentence you will have to do some parsing, google "C++ split" or something similar, most likely you will use stringstream and the add that to the stack.
4) Now just pop the stack and output the value popped

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

You posted Code that makes no sense at all.

1) Output a message
2) define a string variable
3) Sort nothing since all you did was declare a variable
4) declare a prototype for a non-existant function
5) start a loop the 
   a) inputs a string
   b) outputs the string

You need to think through what you want to do and put the steps in their proper order.


Thanks but i am new to C++; therefore, it is a little hard for me to grasp. But thanks for your insight

hawita
Light Poster
39 posts since Feb 2011
Reputation Points: 7
Solved Threads: 0
 

A stack is a virtual container that behaves something like an array but has restricted, instead of random access, to whats inside.

The first thing you need to be able to do is be able to identify separate words (also known as tokens or substrings or whatever). Using the enter key and accepting input with the >> operator is one way to get single words, though input isn't necessarily restricted to one word at a time. For example, you can also read in an entire line consisting of multiple words separated by a space (using getline())and then find the spaces to separate the individual words. Finding the spaces can be done several diffent ways, depending on what you know how to do.
1) You could evaluate each char in the input line one at a time. If it isn't a space you add it to a new char array (word)and when you find a space you add a null terminating char to the end of the new char array and then go on to a new char array (word).
2) You could use one of the find() method of the STL string class if you know about STL strings.
3) You could use strtok().
4) You could use a stringstream.
And there may well be other ways to find individual words in a line of input, too.

The point is we don't know what tools you have available. Use what your education has provided you so far. If you are supposed to strike out on your own and find what you can find to do the trick, then pick and choose from one of the above and look them up in your favorite reference material, posting specific questions about a specific process if you have to.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Okey Thanks I made some changes but the program is still not running . Please help :(

#include<iostream.h>
#include<string>
using namespace std;

main() {
     std::cout << "Please enter a sequence of words: ";
     std::string y;  // read words and write them one per line
   
void reverseArrayElement( string data[], int begin, int end); // reverse the elements of a srting array

 while(cin >> y) {
    std::cout << y<< endl;}
  int words;  
 for ( words = 0; words==y ; words--) {
     
        cout << y[words] << endl;
    }


//    begin is the index of the first element in the range to be reversed, and end is the index after the last element in the range

system("Pause");

}
hawita
Light Poster
39 posts since Feb 2011
Reputation Points: 7
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You