Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strtok to a reversible array !!!

Status
Not open for further replies.

tayse

Technical User
Jan 10, 2006
9
CA
Hi,

Before posting anything here I searched google and the forum but unfortunately could find a solution.

The problem is, user enters max. 80 characters, program reads the input and reverse the sentence.

Example: Input is "This is just an example"
Output should be "example an just is this"

Here is the code,

#include<iostream>
#include<string>
#include<cstring>
using namespace std;

int main()
{
char mystring[80];
char *p;
int count=0, i,j=0;


cout<<"Enter a sentence up to 80 characters \n\n";
cin.getline (mystring,80);

for (i=0;i<80;i++)
{
if (mystring==' ')
count=count+1;
}

int *array = new int [count];


cout << "The string is: [" << mystring << "]\n";

p = strtok(mystring, " ");
*(array+j)=*p;

while ( p ) {
cout << "\t" << p << "\n";
p = strtok(NULL, " ");
j++;
*(array+j)=*p;
}

cout<<"array+j= "<<(array+j)<<endl;

delete [] array;

return 0;
}

Other than this code, I tried some different things but none gave me what I needed. This code even gives an error on windows XP.

Appreciate for any help.
 
Please use [code][/code] tags when posting code.

> This code even gives an error on windows XP.
Maybe this line?
> int *array = new int [count];

What are you trying to store here?
Char pointers from the look of things, yet you said int.

Maybe
char **array = new char *[count];

> cout<<"array+j= "<<(array+j)<<endl;
Now use another for loop to count backwards from j to 0

--
 
I would get rid of the arrays and use an STL stack instead. Then you can push each word onto the top of the stack and pop them off in reverse order.
 
int *array = new int [count];

count is the number of spaces in a sentence so that I could make an array with the excat number of rows...

I took a C++ class 2 years ago and now I am taking this advanced C++ course and they, teachers, assume that we remember everything.

I am sure this is a very simple program to code but...

Sorry, I do not know what STL stack is!

Thanks for the effort...
 
STL strings and stacks are easy to use:

Here's an example:
Code:
string strIn;
getline( cin, strIn );  // Get input from keyboard.

stack<string> words;
string word;

size_t start = 0;
size_t end   = 0;

while ( start != string::npos )
{
   end  = strIn.find( " ", start );
   word = strIn.substr( start, end - 1 );
   words.push( word );

   if ( end != string::npos )
   {
      ++end;
   }

   start = end;
}
...
Then use stack functions top() and pop() to get the words back in reverse order.
You may also want to add extra code to handle unexpected cases like typing multiple spaces in a row or using tabs...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top