Word sort using strtok

This is a discussion on Word sort using strtok within the C Programming forums, part of the General Programming Boards category; Hi everyone I'm starting to learn C programming and I have a bad time in making a program that sort ...

  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 08: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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,915
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,915
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    1,194
    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, 11:52 AM
  4. Word Sort--Again, but this time in C++
    By abh!shek in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2008, 11:19 AM
  5. strtok - word counting from a file
    By |PiD| in forum C Programming
    Replies: 4
    Last Post: 11-15-2002, 03:16 AM

Tags for this Thread


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