Thread: Arrange set of names in an array in alphabetical order.

  1. #1
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79

    Arrange set of names in an array in alphabetical order.

    I am having problem with comparing first letter of every wordso that i can arrange them in array.What am i doing wrong?(i am new at this)

    Code:
    /*22/10/13 15:30
      Arrange set of names in an array in alphabetical order
    */
    #include<stdio.h>
    main( )
    {
     int x,a,i=0,j;
     char *temp, *str[]={
    				 "nitin",
    				 "saurabh",
    				 "amit",
    				 "himanshu"				 
    				};
     for(j=0;j<3;j++)
      {
        i = j+1;
       for (;i<4;i++)
        {
         a=check(str[j],str[i]);
         if(a==2)
          { 
           temp=str[j];
           str[j]=str[i];
           str[i] = temp; 
          }
        }
      }
     for(x=0;x<4;x++)
     printf("\n%s",str[x]);
    }
    
    
    check(char *a,char *b)
    {
     if(*a >*b)
     {
      return(2);
     }
    }
    i am getting unexpected output here :
    Code:
    himanshu
    amit
    nitin
    saurabh

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Consider using a better compiler, with better warnings.
    Code:
    $ gcc -Wall baz.c
    baz.c:5:1: warning: return type defaults to ‘int’ [-Wreturn-type]
    baz.c: In function ‘main’:
    baz.c:19:6: warning: implicit declaration of function ‘check’ [-Wimplicit-function-declaration]
    baz.c: At top level:
    baz.c:33:1: warning: return type defaults to ‘int’ [-Wreturn-type]
    baz.c: In function ‘check’:
    baz.c:39:1: warning: control reaches end of non-void function [-Wreturn-type]
    baz.c: In function ‘main’:
    baz.c:30:1: warning: control reaches end of non-void function [-Wreturn-type]
    Now ask yourself what would happen if check still returned 2 just because it fell off the end of the function.
    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.

  3. #3
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    which compiler is good according to you?suggestions...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    It's right there in the command line.

    If you're a windows user, then Code::Blocks offers an excellent IDE and GCC combination.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. QuickSort Char Array/String (Alphabetical order)
    By ChickenChowMinh in forum C Programming
    Replies: 1
    Last Post: 02-10-2013, 04:23 PM
  2. Replies: 4
    Last Post: 04-21-2012, 10:31 PM
  3. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  4. bubbleSort array string in alphabetical order.
    By martyx in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2010, 11:17 PM
  5. names in alphabetical order?
    By n3cr0_l0rd in forum C Programming
    Replies: 21
    Last Post: 02-06-2009, 08:59 PM