# Putting a word in alphabetical order.

• 12-13-2006
Brewer
Putting a word in alphabetical order.
I'm trying to put a word into alphabetical order (i.e. so that the letters contained within are in alphabetical order), but do you have any idea how I would do this with a word of unknown length?
I can think how to do it with one character and the character next to it using a for loop, but not so it checks the other letters in the sequence against it.

If you have any ways about how to do this then I would appreciate it.

For example I'm trying to write it so that:
ball would become abll, but also so that program would become agmoprr, and so on and so forth for any length word.
• 12-13-2006
Kennedy
bubble sort/quick sort/select sort. <<-- use google and search for the code. Quick sort is available already via stdlib.h, but I don't think you should use that for your class.
• 12-13-2006
dwks
Wikipedia's list of sorting algorithms: http://en.wikipedia.org/wiki/Sorting_algorithm

Here are some of the simpler ones:
• 12-13-2006
ssharish2005
Code:

```for(i=0;i<strlen(str)-2;i++)     {         for(j=i+1;j<strlen(str)-1;j++)         {             if(str[i] > str[j])             {                 temp = str[i];                 str[i] = str[j];                 str[j] = temp;             }         }     }```
this code is just to start you up

ssharish2005
• 12-13-2006
dwks
Uh huh . . . besides the fact that this is very likely homework, you're calling strlen() to calculate the length of the string for every iteration of the loop, which is very expensive in CPU time. Save the result.
Code:

`size_t len = strlen(str);`
• 12-13-2006
ssharish2005
DWKS u are right, has kills the CPU time. :)

Thanks

ssharish2005
• 12-14-2006
Brewer
Yeah he introduced Bubble Sort to us today (well he actually described what it is, and we had to implement it), so I reckon that that is the right way to attack this problem. Thanks for your help guys.
• 12-14-2006
Brewer
Right so I've made the swap thing into a function and my code is as follows:
Code:

```char swap2(char word2, int length) {     char temp[1];     int i;     int j;     for(i=0; i<(length-2);i++)     {               for(j=i+1; j<(length-1); j++)               {                         if(word2[i] > word2[j])                         {                             temp=word2[i];                             word2[i]=word2[j];                             word2[j]=temp;                           }                 }       } }```
But it won't compile. Any ideas why? I have a feeling I need to use strcpy, for swapping the characters around, but its not picking up the individual characters (I don't think). Any pointers?
• 12-14-2006
Happy_Reaper
shouldn't word2 be a char* ?
• 12-14-2006
Kennedy
Also, you've made your function return a char, but you don't return anything. Make the function void or return 0; or something.
• 12-14-2006
vart
and seems to be that
char temp[1];

should be
char temp;
from how do you use it
• 12-15-2006
ssharish2005
Quote:

But it won't compile
If u copy and paste it, that would be the result u will get. Study the logic and then code

ssharish2005
• 12-16-2006
dwks
Quote:

Originally Posted by ssharish2005
Code:

```for(i=0;i<strlen(str)-2;i++)     {         for(j=i+1;j<strlen(str)-1;j++)         {             if(str[i] > str[j])             {                 temp = str[i];                 str[i] = str[j];                 str[j] = temp;             }         }     }```
this code is just to start you up

ssharish2005

That's kind of misleading. Look at this, from http://en.wikipedia.org/wiki/Bubble_sort:
Code:

```function bubble_sort(list L, number listsize)     loop         has_swapped := 0 //reset flag         for number i from 1 to (listsize - 1)             if L[i] > L[i + 1] //if they are in the wrong order                 swap(L[i], L[i + 1]) //exchange them                 has_swapped := 1 //we have swapped at least once, list may not be sorted yet             endif         endfor         //if no swaps were made during this pass, the list has been sorted         if has_swapped = 0             exit         endif     endloop endfunction```
And see, the OP's copied it . . .
Code:

`if(word2[i] > word2[j])`
Perhaps it may work as you have it, but even so it doesn't use the Bubble Sort algorithm.

 Remember, if you access a variable like so
Code:

`variable[x]`
it must be an array (or a pointer). [/edit]