Thread: Sorting Elements in Lexicographical Order

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    108

    Sorting Elements in Lexicographical Order

    Hey guys,
    I have a code here i'm using for an exercise and basically it sorts a group of words of the user's choice and prints them in Dictionary order.

    I have a few questions as I'm new to C programming and it's my first year of Computer Science.

    First of all here's the code:
    ( sorry the code is like this.. don't know how to post it better )
    Code:
    1 #include<stdio.h>
    2 #include <string.h>
    3 int main(){
    4    int i,j;
    5    char str[10][50],temp[50];
    6    printf("Enter 10 words:\n");
    7    for(i=0;i<10;++i)
    8        gets(str[i]);
    9    for(i=0;i<9;++i)
    10        for(j=i+1;j<10 ;++j){
    11            if(strcmp(str[i],str[j])>0)
    12            {
    13                strcpy(temp,str[i]);
    14                strcpy(str[i],str[j]);
    15                strcpy(str[j],temp);
    16            }
    17        }
    18    printf("In lexicographical order: \n");
    19    for(i=0;i<10;++i){
    10        puts(str[i]);
    21    }
    22    return 0;
    23 }

    1st Question: Line-7 the for loop explains that user's printing 10 words no more (True/False)
    2nd Question: Line-9 & 10 don't understand this loop. Can someone explain me?
    3rd Question: Now i understand that the user needs to print 10 words after hitting "Enter" but how to change this code so that the user can insert a serie of words using spaces instead of pressing "Enter"?
    I want it to scan only the first line that contains my group of words.
    example: john jordan jake yann max kevin
    instead of:
    john
    jordan
    jake
    yann
    max
    kevin
    Last edited by YannB; 01-14-2013 at 01:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-26-2011, 10:01 AM
  2. sorting ABC order
    By rodrigorules in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2010, 01:37 AM
  3. Replies: 8
    Last Post: 10-21-2007, 01:38 PM
  4. printing array elements in ascending order
    By galmca in forum C Programming
    Replies: 29
    Last Post: 10-24-2004, 11:24 PM
  5. Sorting in Alphabetical order
    By Andre Santiago in forum C Programming
    Replies: 1
    Last Post: 12-13-2002, 06:14 PM