Thread: strtok

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    10

    reverse string

    I know this was a long time ago this was posted but I was hoping someone could help. I know how to use strtok but after everything is tokenized how would I print everything in reverse order. If someone could give me a quick explanation on that code that would be great. I am trying to do this for college and I am totally stuck. This would help me on other assignments I have also. Thanks

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Avoid posting in really old threads (this is 3 years old! I was a little kid 3 years ago!) Just make a new thread.

    Code:
    	// Our variables
    	char s[] = "hello people of the world";
    	char *parts[50];
    	int i = 0;
    
    	// Tokenize it
    	parts[i] = strtok(s, " ");
    	while(parts[++i] = strtok(NULL, " "));
    
    	// Print it out in reverse
    	for(; i >= 0; i--)
    		printf("%s\n", parts[i]);

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    10

    sorry

    I found this site under desperate moments and just realized that is what the "announcements" had said. I will make sure I abide by that the next time. Wow, this seemed really easy. Why couldn't I figure this out? Thanks for your help and quick response.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 10:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM