Thread: Sort Strings in Alphabetical Order

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    Sort Strings in Alphabetical Order

    I want to short string in Alphabetical Order

    Code:
    #include <stdio.h> int main(void)
    {
        char string[10] [10], temp;
    	int i, j, n;
     
        printf ("\n Pramot user to Enter size: ");
        scanf ("%s", &n);
       
          for (i = 0; i < n; i++)
          {
            printf ("\n Enter the  string: ");
            scanf ("%s", &string[i]);
          }
       
           for(i = 0;i < n;i++)
          {
         
            for( j = i; j < n; j++)
            {
                 if(strcmp(string[i],string[j])>0)
    			 {
                   strcpy(temp,string[i]);
                   strcpy(string[i],string[j]);
                   strcpy(string[j],temp);   
                 }
            }
           
          }
         
          for(i=0;i<n;i++)
                 
          printf("%s ",string[i]);
       
     
          return 0;
    }
    hello.c: In function 'main':
    hello.c:21:17: warning: implicit declaration of function 'strcmp' [-Wimplicit-function-declaration]
    if(strcmp(string[i],string[j])>0)
    ^~~~~~
    hello.c:23:16: warning: implicit declaration of function 'strcpy' [-Wimplicit-function-declaration]
    strcpy(temp,string[i]);
    ^~~~~~
    hello.c:23:16: warning: incompatible implicit declaration of built-in function 'strcpy'
    hello.c:23:16: note: include '<string.h>' or provide a declaration of 'strcpy'
    hello.c:23:23: warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]
    strcpy(temp,string[i]);
    ^~~~
    hello.c:23:23: note: expected 'char *' but argument is of type 'char'
    hello.c:25:33: warning: passing argument 2 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]
    strcpy(string[j],temp);
    ^~~~
    hello.c:25:33: note: expected 'const char *' but argument is of type 'char'

    What's the wrong

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Try adding #include <string.h> to the top of your source code

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Temp needs to be an array as well.
    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.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by Salem View Post
    Temp needs to be an array as well.
    Code:
    #include <stdio.h>#include <string.h>
     int main(void)
    {
        char string[10] [10], temp[10];
        int i, j, n;
      
        printf ("\n Pramot user to Enter size: ");
        scanf ("%s", &n);
        
          for (i = 0; i < n; i++)
          {
            printf ("\n Enter the  string: ");
            scanf ("%s", &string[i]);
          }
        
           for(i = 0;i < n;i++)
          {
          
            for( j = i; j < n; j++)
            {
                 if(strcmp(string[i],string[j])>0)
                 {
                   strcpy(temp[i],string[i]);
                   strcpy(string[i],string[j]);
                   strcpy(string[j],temp[i]);   
                 }
            }
            
          }
          
          for(i=0;i<n;i++)
                  
          printf("%s ",string[i]);
        
      
          return 0;
    }
    hello.c: In function 'main':
    hello.c:24:23: warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]
    strcpy(temp[i],string[i]);
    ^~~~
    In file included from hello.c:2:0:
    c:\mingw\include\string.h:79:40: note: expected 'char *' but argument is of type 'char'
    _CRTIMP __cdecl __MINGW_NOTHROW char *strcpy (char *, const char *);
    ^~~~~~
    hello.c:26:33: warning: passing argument 2 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]
    strcpy(string[j],temp[i]);
    ^~~~
    In file included from hello.c:2:0:
    c:\mingw\include\string.h:79:40: note: expected 'const char *' but argument is of type 'char'
    _CRTIMP __cdecl __MINGW_NOTHROW char *strcpy (char *, const char *);

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    What type is temp[i]? Is it a pointer to char, or is it a char? What type does strcpy() want for its first argument on line 23? What type does strcpy() want as its second argument on line 25?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why did you feel it was necessary to subscript temp?
    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. Replies: 3
    Last Post: 04-12-2015, 11:54 AM
  2. Replies: 2
    Last Post: 10-15-2013, 12:12 PM
  3. Replies: 4
    Last Post: 04-21-2012, 10:31 PM
  4. Sorting strings into alphabetical order
    By dude_tron_1982 in forum C Programming
    Replies: 5
    Last Post: 12-10-2010, 12:34 PM
  5. String sort in alphabetical order
    By ThLstN in forum C Programming
    Replies: 6
    Last Post: 01-06-2008, 02:59 AM

Tags for this Thread