Thread: String ordering with inserstion

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    95

    String ordering with inserstion

    I'm trying to sort strings as alphabetical but somewhere i have error. Where is my wrong ? Thank you for all appreciated answers.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(){
    
    
    	char s[100][20];
    	int num;
    	char temp[20];
    	int i,j;
    
    
    	printf("Enter total elements: ");
       	scanf("%d", &num);
    
    
    
    
        printf("\nEnter any num strings : ");
        for (i = 0; i < num; i++)
          scanf("%s", s[i]);
    
    
       	for(i=1;i<num;i++){
       		strcpy(temp,s[i]);
       		j=i-1;
       		while( (0 <= j) && ( 0 < strcmp(s[i],temp) ) )
       		{
       			strcpy( s[j+1],s[j] );
       			j=j-1;
       		}
    
    
       		strcpy( s[j+1],temp );
       	}
    
    
       	printf("\nStrings in order are : ");
       for (i = 0; i < num; i++)
          printf("\n%s", s[i]);
    
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Are you trying to implement the string version of insertion sort?

    If so I'd suggest using pointers to strings instead of a 2d array. It'd save costly processing time by eliminating calls to strcpy(), as you can re-direct pointers to the desired sort order. And below is your modified code, although it lacks some good-to-have features:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
            char s[100][20];
            char temp[20];
            int i, j, num;
    
            printf("Enter total elements: ");
            scanf("%d", &num);
    
            printf("\nEnter any num strings : ");
            for (i = 0; i < num; i++) {
                scanf("%s", s[i]);
                for (j = i; j > 1; j--)
                    if (strcmp(s[j], s[j-1]) < 0) {
                        strcpy(temp, s[j]);
                        strcpy(s[j], s[j-1]);
                        strcpy(s[j-1], temp);
                    }
            }
    
            printf("\nStrings in order are : ");
            for (i = 0; i < num; i++)
                printf("\n%s", s[i]);
    
            return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ordering C++ strings
    By Vespasian in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2013, 05:45 AM
  2. Is is possible to specify the ordering of a std::map ?
    By manasij7479 in forum C++ Programming
    Replies: 26
    Last Post: 08-24-2011, 01:42 PM
  3. ordering characters
    By 12thman in forum C Programming
    Replies: 3
    Last Post: 11-02-2010, 07:15 AM
  4. key ordering in map
    By m37h0d in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2008, 08:28 AM
  5. Ordering string by alphabetical order
    By Scarvenger in forum C++ Programming
    Replies: 9
    Last Post: 09-16-2006, 08:58 PM