Reversing strings using strtok()

This is a discussion on Reversing strings using strtok() within the C Programming forums, part of the General Programming Boards category; What i gotta do: Write a program that inputs a line of text, tokenizes the line with the strtok() function ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    24

    Reversing strings using strtok()

    What i gotta do:

    Write a program that inputs a line of text, tokenizes the line with the strtok() function and outputs the tokens in reverse order. For example the input string: "The quick brown fox jumps over the dog" would be output as: "dog the over jumps fox brown quick The".

    Test your solution with the following test data:

    This is, the test data for the program

    The output should be:

    program
    the
    for
    data
    test
    the
    is
    This


    Code so far:
    Code:
    int main()
    {
    	char s1[80];
    	char *tokenptr;
    	char seps[] = " ,\t\n';
    	char *tokenarray[80];
    	int i = 0:
    
    	printf("Input the string you want tokenized: \n\n");
    	gets(s1);
    
    	printf("\nThe tokens in reverse order are: \n\n");
    	tokenptr = strtok(s1, seps);
    
    	while(tokenptr != null){
    		tokenarray[i++] = s1;
    		printf("%s\n", tokenptr);
    		tokenptr = strtok(null,seps);
    	}
    
    	return 0;
    }
    it works but i need it to be in reverse order

    my output looks like:
    This
    is,
    the
    test
    data
    for
    the
    program


    need it to be:

    program
    the
    for
    data
    test
    the
    is
    This

    Any suggestions?

  2. #2
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Well, They're being outputted in the right order aren't they? So, you need to output them in reverse order. There are a few ways to do this. A simple one would be to create an array of tokenptr's, each one pointing to a different token of the string. Then just cycle through the tokenptr array, in reverse, printing each time. You'd merely need to add a counter to keep track of how many tokes there are. That way, you know which member of the tokenptr array to start on when you cycle through in reverse.

    Also, I noticed you are using "null" quite a lot. I don't know where you picked that up, but I have never seen "null" used before. Only NULL. I can assume, since things SEEM to be working correctly, that null is accepted by your compiler. But null is not a part of standard C and you should always use NULL. Unless your professor forces you to, in which case, just humor him until you pass.
    Code:
    void function(void)
     {
      function();
     }

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    well a no go ...need some help

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    remember how many splitted string there were and put every pointer returned by strtok in an array.
    Then after all strtok's are done loop from count to zero and output all the strings in the array.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    > gets(s1);
    *sharp intake of breath* - read the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    got er thx....

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, 04:48 PM
  2. Reversing Strings
    By Halo in forum C++ Programming
    Replies: 6
    Last Post: 03-18-2006, 02:58 PM
  3. Breaking strings without using strtok()
    By hykyit in forum C Programming
    Replies: 2
    Last Post: 08-23-2005, 05:40 AM
  4. Replies: 1
    Last Post: 09-05-2004, 06:42 PM
  5. reversing strings
    By mcorn in forum C++ Programming
    Replies: 14
    Last Post: 10-13-2002, 01:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21