Thread: Problem with sorting a string!!

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    103

    Problem with sorting a string!!

    Hi everyone!
    Can any one tell what wrong with this code, I mean when i insert the printf function within the for loop (the i loop) it lists me all the steps correclty. But why it doesn't display the string sorted in one shot using the last printf..that what i want to do..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 24
    
    int main(){
               char x[SIZE];
               int i, j, t = 0;
               gets(x);
               for (i=1; i<SIZE; i++){
                         t=x[i];
                      for (j = i; j>0 && x[j-1]>t ; j--){
                          x[j]=x[j-1];
                      }
                      x[j]=t;
                      }     
               printf("%s\n",x);
               system("PAUSE");
               return(0);         
    }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    No response??

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    First of all, if you use gets and the user enters more than twentyfour chars you will get a servere error (probably segmentation fault).

    Second of all, the line "int i,j,t = 0" will initate t to 0 while it doesn't initiate i and j, which will make i and j random numbers. I ran the part with the initation and printed out i,j and t and got this output:

    i: -1209711522
    j: -1209042711
    t: 0

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Can you say what it's ultimately trying to do? I am having a hard time figuring it out. If I am thinking correctly, I might know how to fix it, but I just need a simple description of what your trying to accomplish.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    i, j, and even t don't actually need to be initialised, since they're assigned upon first use.
    I would change the type of t to a char though.

    The problem is that the outer loop of your insertion sort goes up to i<SIZE, but you didn't necessarily type in SIZE charcters. In fact if you had typed in SIZE characters you'd have a buffer overrun.
    Since you're sorting ascending, the null char in the string is being put before the printable chars since it has the lowest value, assuming chars are compiled as unsigned.

    Use i<strlen(x) instead in the for loop, or assign strlen(x) to a variable first, and use that.
    Last edited by iMalc; 05-03-2008 at 07:01 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    I'd say it's a bubblesort. I got it working with some changes.

    To get the input, I'd do the following. This will avoid getting Segmentation faults, and you will still get the input until the returnchar or the buffer is full:

    Code:
      int x=0;
      int size=0;
    
      char* buffer;
      buffer=(char*)malloc(255*sizeof(char)); // or SIZE instead of 255...
      if (buffer==NULL)
      {
          perror("Malloc");
          exit(0);
        }
      ch=getc(stdin);
      while((ch!=10)&&(x<255)) // And SIZE here aswell if you use it above
        {
          buffer[x]=ch;
          ch=getc(stdin);
          x++;
        }
    size=x;

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    i and j don't need to be initialised, they're assigned upon first use.
    I see your point. The strange thing is that I got it working when I initialized i and j. But then again, I counted so I input exactly 24 chars. More than that it won't work, less, I'm not sure.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Zarniwoop View Post
    I'd say it's a bubblesort. I got it working with some changes.
    You could say that, but you'd be wrong. It is Insertion Sort (ascending); I'm not guessing.
    To get the input, I'd do the following. This will avoid getting Segmentation faults, and you will still get the input until the returnchar or the buffer is full:

    *** code snipped ***
    For data input I suggest simply using scanf. Read this
    Last edited by iMalc; 05-03-2008 at 09:54 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Hi,
    I'm sorry for not replying yesterday but i live in morocco, so you may easily figure out that i was sleeping while you were discussing that.
    Actually, I'm not allowed to use neither alloc. And yes it is insertion sort...
    here is some further implementation, but the problem still persists.
    PS: for those how see the thread for the first time, the problem is that the last printf doesn't display the string sorted..why?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 24
    
    int main(){
               char x[SIZE];
               int i, j,n;
               char t ;
               gets(x);
               for (i=1 , n = strlen(x); i < n ; i++){
                         t=x[i];
                      for (j = i; j>0 && x[j-1]>t ; j--){
                          x[j]=x[j-1];
                      }
                      x[j]=t;
                      }     
               printf("%s\n",x);
               system("PAUSE");
               return(0);

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    works for me
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 24
    
    int main()
    {
    	char x[SIZE];
    	int i, j,n;
    	char t ;
    	char* end;
    	fgets(x,sizeof x,stdin);
    
    	end = strchr(x,'\n');
    	if(end) *end = '\0';
    
    	for (i=1 , n = strlen(x); i < n ; i++)
    	{
    		t=x[i];
    		for (j = i; j>0 && x[j-1]>t ; j--)
    		{
    			x[j]=x[j-1];
    		}
    		x[j]=t;
    	}     
    	printf("&#37;s\n",x);
    	return(0);
    }
    result
    Code:
    a;lsdkfjqwpeoriuxlvn
    ;adefijkllnopqrsuvwx
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Im not allozed to use any other functions except the ones in my code, normally it sould work, but i just redisplay the string NOT sorted...any comment about my code??

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Sorry allowed not allozed

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Any??

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What do you mean not sorted? Show sample input and output you get?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok thanks anybody, Problem solved!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM