Thread: Bubble-sort C

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Bubble-sort C

    rite a function that when invoked as bubble_string(s) causes the characters in the string s to be bubble-sorted. If s contains the string "xylophone", then the following statement should cause ehlnoopxy to be printed.
    The errors I get are: 10.4.c: In function `main':
    10.4.c:8: error: invalid use of void expression
    10.4.c: In function `bubble_string':
    10.4.c:17: error: syntax error before ')' token
    Any ideas on how to fix this?

    Code:
    
    #include <stdio.h>
    void swap (char*, char*);
    void bubble_string(char s[]);
    
    int main(void)
    {
            char *s= "xylophone";
            printf("%s", bubble_string(s));
    
    return 0;
    }
    
    void bubble_string(char s[])
    {
            char i, j, n;
            n  = strlen(s);
            for(i = 0)
                    for(j = n - 1)
                            if(s[j-1] > s[j])
                                    swap(&s[j-1], &s[j]);
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your for() statements are incorrectly composed... look it up in you C library documentation.
    The swap() function is declared but never defined.
    You should not pass your string s into bubble_string() as an array, use a pointer (char *s) instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using bubble sort to sort a matrix by sum..
    By transgalactic2 in forum C Programming
    Replies: 22
    Last Post: 12-23-2008, 12:03 AM
  2. Bubble sort
    By Loooke in forum C Programming
    Replies: 9
    Last Post: 12-08-2004, 02:29 PM
  3. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  4. Need Help With This Bubble Sort
    By Shahram_z in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2003, 01:34 PM
  5. a little help with bubble sort
    By cman in forum C Programming
    Replies: 3
    Last Post: 02-06-2003, 03:45 AM