Putting a word in alphabetical order.

This is a discussion on Putting a word in alphabetical order. within the C Programming forums, part of the General Programming Boards category; I'm trying to put a word into alphabetical order (i.e. so that the letters contained within are in alphabetical order), ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    22

    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.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    DWKS u are right, has kills the CPU time.

    Thanks

    ssharish2005

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    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.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    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?

  9. #9
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    shouldn't word2 be a char* ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  10. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Also, you've made your function return a char, but you don't return anything. Make the function void or return 0; or something.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,452
    and seems to be that
    char temp[1];

    should be
    char temp;
    from how do you use it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

    [edit] Remember, if you access a variable like so
    Code:
    variable[x]
    it must be an array (or a pointer). [/edit]
    Last edited by dwks; 12-16-2006 at 04:14 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game and strcmp
    By crazygopedder in forum C Programming
    Replies: 12
    Last Post: 11-23-2008, 05:13 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. Passing structures to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 06:13 AM
  5. Putting numbers in order
    By Queatrix in forum C++ Programming
    Replies: 7
    Last Post: 05-01-2005, 01:00 PM

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