ordering characters

This is a discussion on ordering characters within the C Programming forums, part of the General Programming Boards category; instead of reordering it, it just flips it. i'm not sure where i went wrong. #include <stdio.h> void order_chars( char ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    ordering characters

    instead of reordering it, it just flips it. i'm not sure where i went wrong.


    #include <stdio.h>

    void order_chars( char *c1, char *c2, char *c3)
    {
    char temp;
    printf("%c %c %c\n", c1, c2, c3);
    if(c2 < c1 && c2 < c3)
    {
    temp = *c1;
    *c1 = *c2;
    *c2 = temp;
    }
    else if(c3 < c1 && c3 < c2)
    {
    temp = *c1;
    *c1 = *c3;
    *c3 = temp;
    }
    else
    ;
    printf("%c %c %c\n", c1, c2, c3);
    if(c2 < c3)
    {
    temp = *c2;
    *c2 = *c3;
    *c3 = temp;
    }
    printf("%c %c %c\n", c1, c2, c3);
    }

    int main (void)
    {
    char c1,c2,c3;
    printf("Input three unordered characters\n");
    scanf("%c%c%c",&c1,&c2,&c3);
    printf("%c %c %c\n", c1, c2, c3);
    order_chars(&c1,&c2,&c3);
    printf("%c %c %c", c1, c2, c3);
    return 0;
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Use code tags.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    i'm sorry, i'm new to this, i didn't know how.

    Code:
    #include <stdio.h>
    
    void order_chars( char *c1, char *c2, char *c3)
    {
        char temp;
        printf("%c    %c    %c\n", c1, c2, c3);
        if(c2 < c1 && c2 < c3)
        {
            temp = *c1;
            *c1 = *c2;
            *c2 = temp;
        }
        else if(c3 < c1 && c3 < c2)
        {
            temp = *c1;
            *c1 = *c3;
            *c3 = temp;
        }
        else
            ;
        printf("%c    %c    %c\n", c1, c2, c3);
        if(c2 < c3)
        {
            temp = *c2;
            *c2 = *c3;
            *c3 = temp;
        }
        printf("%c    %c    %c\n", c1, c2, c3);
    }
    
    int main (void)
    {
        char c1,c2,c3;
        printf("Input three unordered characters\n");
        scanf("%c%c%c",&c1,&c2,&c3);
        printf("%c    %c    %c\n", c1, c2, c3);
        order_chars(&c1,&c2,&c3);
        printf("%c    %c    %c", c1, c2, c3);
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    If you're going to compare the characters
    Code:
    if(c2 < c1 && c2 < c3)
    you'd have to do
    Code:
    if(*c2 < *c1 && *c2 < *c3)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Replies: 2
    Last Post: 09-16-2009, 07:00 AM
  3. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 07:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM

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