Thread: Alphabetical orders using string

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    19

    Alphabetical orders using string

    Im just a beginner btw so i need help how to do this
    Ask 3 strings and arrange them in alphabetical order:


    Code:
    #include <stdio.h>
    #include <conio.h>
    
    main()
    {
    char b,c,d;
    char a[i] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    
    printf ("Input a letter");
    scanf("%c",&a);
    
    printf ("Inpute letter");
    scanf("%c",&b);
    
    printf ("Input letter");
    scanf ("%c",&c);
    
    for (i = 0; i < 3; i++)
    {
    printf ("%c", a[i]);
    printf ("\n");
    }
    
    printf ("%c %c %c", a, b , c);
    getch();
    }

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    1) Read in the 3 strings using a combination of fgets and sscanf
    2) Now compare their first characters s1[0], s2[0] and s3[0]. If that orders them then output the result. If it doesn't go to the next character and continue until it does.

    That's it. By the way, next time you start a thread, you may want to be asking a more specific question.
    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

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why not use strcmp()?

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    You have a start there but I think you might (should) be getting some errors? (I sure did)
    This should get you running:
    Code:
    /* orderstrings1.c */
    
    #include <stdio.h>
    #include <conio.h>
    
    /*  use to pause OR to  empty leftovers in stdin buffer  */
    void PAUSE(void){int c; while((c = getc(stdin)) != '\n' && c != EOF); }
    
    /*  use instead of scanf("&#37;c", &char)  and getchar()     */
    int mygetchar(void){ int c;   c = getc(stdin);   PAUSE();   return c; }
    
    int main()
    {
      char b, c, d;
      /*char a[i] = {'.... You can't do that unless i has a value
      char a[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
      Note the '\0' at end
      ...works but this does the same thing with less effort:
                             (and how about some lower case letters too) */
      char a[] = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
      int i = sizeof(a) - 1;   /* get the length of the string and stor in i */
    
      printf("i= %d \n", i);
    
      /* I don't think you want to put anything else in 'a' do you? maybe in 'd' ?
      printf ("Input a letter");
      scanf("%c",&a);
      */
    
      /* printf ("Input a letter");    I added ': '  to improve appearance */
      printf ("Input a letter: ");
      scanf("%c",&b);
      PAUSE();
      /* CAUTION:  Using scanf() for %c haracters is overkill and causes another
         problem.  Scanf() will read the first char it encounters and leave any
         other input in the input buffer which includes the \n from hitting return
         key when your finished entering.  That \n will be picked up by the next
         call to scanf() and so it being satisfied with \n as it's character it
         will end leaving you with a \n as value of c.  I made a PAUSE(); function
         (see at top) which will clean that up.
         I also left a 'mygetchar()' to use instead of scanf(%c) or regular getchar() 
         which will also leave unused chars in the stdin buffer.  */
    
      printf ("Input a letter: ");
      c = mygetchar();
    
      printf ("Input a letter: ");
      d = mygetchar();             /* I added this for unused variable 'd' */
    
      /* ok so what do you want to do here???? Your move... */
      for (i = 0; i < 3; i++)
      {
        printf ("%c", a[i]);
        printf ("\n");
      }
    
      /* printf ("%c %c %c", a, b, c);  again with the string a and no d */
      printf ("%c %c %c    ", b, c, d);
    
      /* getch();   this will do the same thing and you won't need conio.h */
      PAUSE();
    
      return 0;
    }
    So you want to alphabetize strings? You will need to input strings then.
    Happy's suggestions look good for that.
    Last edited by HowardL; 09-30-2007 at 11:05 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Is redefining the alphabet an attempt to tell the compiler how you want some strings ordered?
    An analogy to that: Someone put 3 objects in front of you and asked you to sort them from smallest to largest, left to right, you just drew a picture of all sorts of objects of increasing sizes. To actually sort things you were given have to actually do the work of moving those things, rather than just showing "this is what sorted things look like", and waiting for some magic to kick in.

    Try starting with only two objects to sort, and see if you can figure out how to swap them. Maybe start with ints too, instead of strings.
    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
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why not simplify
    Code:
    char a[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    To
    Code:
    char a[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Far easier to read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM