Thread: Word sort using strtok

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    61

    Unhappy Word sort using strtok

    Hi everyone I'm starting to learn C programming and I have a bad time in making a program that sort the words in ascending order using strtok. Like this:

    INPUT: I am good.

    OUTPUT: am
    good
    i

    Please help me, thanks!
    Last edited by programmerc; 12-21-2012 at 09:04 AM. Reason: Typographic error

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post up what you have tried. That fulfills the requirements of the forum, AND it helps us to help you, as well. The doctor must see the patient.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    I do not have a code yet..

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hmm.. Then take your time to write some code and then post back if needed
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the first step is using strtok so you can just do

    INPUT: I am good.

    OUTPUT:
    I
    am
    good

    Then we'll talk about sorting, if you can't figure it out in the meantime.
    A development process
    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.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    Code:
    #include<stdio.h>
    #include<conio>.h>
    void main(){
      int i,j,n;
      char str[20][20],temp[20];
      puts("Enter the no. of string to be sorted");
      scanf("%d",&n);
      for(i=0;i<=n;i++)
          gets(str[i]);
      for(i=0;i<=n;i++)
          for(j=i+1;j<=n;j++){
               if(strcmp(str[i],str[j])>0){
                   strcpy(temp,str[i]);
                  strcpy(str[i],str[j]);
                  strcpy(str[j],temp);
               }
          }
      printf("The sorted string\n");
      for(i=0;i<=n;i++)
          puts(str[i]);
    getch();
    }

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    I didn't use strtok here, because I do not know how to use it in sorting.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You don't use it in sorting. What you probably do is use it prior to the sorting, to break up the sentence into individual words that can be sorted.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    It is possible that I will store the individual words into a new variable?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by programmerc
    It is possible that I will store the individual words into a new variable?
    I expect that you would have an array to store the individual words, or pointers thereof, then you would sort this array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Tokenization example with strtok on the forum

    Need help reading from txt

    You can modify this to fit your problem specification, just sort the words after they are collected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Word Sort Program
    By daggerhunt in forum C Programming
    Replies: 9
    Last Post: 03-20-2012, 01:01 PM
  2. strtok help on a word frequency program
    By steafo in forum C++ Programming
    Replies: 2
    Last Post: 07-25-2010, 09:17 PM
  3. Word Sort
    By abh!shek in forum C Programming
    Replies: 32
    Last Post: 02-04-2008, 12:52 PM
  4. Word Sort--Again, but this time in C++
    By abh!shek in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2008, 12:19 PM
  5. strtok - word counting from a file
    By |PiD| in forum C Programming
    Replies: 4
    Last Post: 11-15-2002, 04:16 AM

Tags for this Thread