Tokens using strtok (c++)

I need help tokenizing a char array and printing them in reverse order

Page 1 of 1

3 Replies - 4478 Views - Last Post: 16 December 2009 - 07:54 AM Rate Topic: -----

#1 Kimibear1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-December 09

Tokens using strtok (c++)

Posted 09 December 2009 - 08:50 PM

ok, so I can break it down into tokens. I need help figuring out how to copy the tokens into an array, and then typing them in reverse order.

For example:

user enters : I love you

program should print: you love I


I also am having issues getting a char array from the user.

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

#include <cstring>
using std::strtok;
using std::strcpy;
using std::strlen;



int main()
{

	char sentence[100];
	char *tokptr;
	char tokes[100];


	cout << "Please enter a sentence to be reversed:\n\n ";
	cin.getline(sentence, 100);

	tokptr = strtok(sentence, " ");

	while(tokptr != NULL )
	{ 
		cout << tokptr << "\n";
		tokptr = strtok(NULL, " ");
	}

	

	strcpy(sentence, tokes);

	

	for(int x = 0; x <= strlen(tokes); x++)
		cout << tokes[x] << " ";

	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Tokens using strtok (c++)

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Tokens using strtok (c++)

Posted 09 December 2009 - 09:28 PM

Why not use a C++ string?
http://www.cplusplus...ference/string/

Iterate through the string from back to front and take a substring when you hit a space. Then print out the substring.

int last = str.length();
for(int i = str.length()-1; i >= 0; i++)
{
  if(str[i] == ' ')
  {
	cout << str.substr(i, last);
	last = i;
  }
}
cout << endl;



I don't know if that code will work it may have a slight logic error as I have no tested it.
Was This Post Helpful? 0
  • +
  • -

#3 MAV_DevWantB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-September 09

Re: Tokens using strtok (c++)

Posted 16 December 2009 - 12:07 AM

View Posteker676, on 9 Dec, 2009 - 08:28 PM, said:

Why not use a C++ string?
http://www.cplusplus...ference/string/

Iterate through the string from back to front and take a substring when you hit a space. Then print out the substring.

int last = str.length();
for(int i = str.length()-1; i >= 0; i++)
{
  if(str[i] == ' ')
  {
	cout << str.substr(i, last);
	last = i;
  }
}
cout << endl;



I don't know if that code will work it may have a slight logic error as I have no tested it.




Salutations,,,,

My name is MAV_DevWantB and I have posted a the exact same program in C forum using "C programming"... I am also having the same problem running threw my "token[i]" array in reverse order so that my line of text is reversed....

Just wondering if you had any luck completing your project... Let me know,, sounds interesting...

thnks
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Tokens using strtok (c++)

Posted 16 December 2009 - 07:54 AM

Alternatively, add the token into another structure to hold, then print it all backwards. As previously mentioned (and I only only say it again for emphasis), there is no reason you should be using C style strings when you're allowed to use many other C++ library features. Use std::string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1