Sort A String Alphabetically.

This is a discussion on Sort A String Alphabetically. within the C Programming forums, part of the General Programming Boards category; Hi all, I want to sort a string alphabetically. For eg: I the input is : I love my country. ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Sort A String Alphabetically.

    Hi all,

    I want to sort a string alphabetically.
    For eg:
    I the input is : I love my country.

    The output should be : I country love my.

    Here is my code. I tried storing each word into a 2-D array. But its showing some errors. Plz help!!!!

    thnx

    Code:
    #include<stdio.h>
    #include<string.h>
    main()
    {
    	int i,j,n,len,len1,k=0,z=0,x;
    	char str[50],temp,a[50][50];
    	printf("Enter the string :");
    	scanf("%[^\n]",str);
    	
    	for (i=0;i<len1;i++) 
    	{
    		if (str[i] != '\0')
    		{
    			a[k][z] = str[i];
    			k++;
    			z++;
    			
    		}
    	}
    	a[k][z]='\0';
    	
    }
    
    for (x = 0; a[x]!='\0'; x++) { }
    		len=i;
    printf("len =%d",len);
    The length is correct. But output I'm getting are some garbage values...

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,702
    One problem is that len1 is uninitialized, leaving the loops exit condition pretty random and probably resulting in a much larger len1 than you expected.

    Anyway, even if len1 was OK, a looks like this.

    a[0][0] = str[0]
    a[1][1] = str[1]
    a[2][2] = str[2]
    ...
    a[len1 - 1][len1 - 1] = str[len1 - 1]

    Which is not contiguous like a string. You should think of it as an array of arrays, so that when you access a[x] it results in the xth word.

    So I guess more like
    Code:
    while (str[i] != 0) {
       if (isspace(str[i])) {
          a[k++][z] = 0;
       }
       else {
          a[k][z++] = str[i];
       }
       ++i;
    }
    Last edited by whiteflags; 02-05-2011 at 12:29 AM.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    54
    The trickiest part of your job is probably managing the memory for the array you'll need to store the words. Unless you'll be able to anticipate the length of the program's input every time it runs, you'll have to have to use dynamic memory allocation, like in this example. In the example on lines 62-65, memory for word storage is allocated dynamically based on a rough word count; the word count is retained for when the allocated memory needs to be freed later on lines 79-81.

    Another thing you should be cognizant of is the possibility of words which begin similarly but have different endings.

    Consider:
    Code:
    Enter string: I love living in my country mang I also like manganese
    For a case sensitive lexicographical ordering, you might get something like this:
    Code:
    I
    I
    also
    country
    in
    like
    living
    love
    mang
    manganese
    my
    Last edited by Boxknife; 02-05-2011 at 01:25 AM. Reason: Correct line numbers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 11:18 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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