Home:ALL Converter>Bubble sort program C

Bubble sort program C

Ask Time:2011-04-16T09:03:36         Author:

Json Formatter

Write 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:3: warning: data definition has no type or storage class 10.4.c: In functionmain': 10.4.c:8: error: syntax error before "char" 10.4.c: In function `bubble_string': 10.4.c:17: error: syntax error before ')' token 10.4.c:18: error: syntax error before ')' token

Any ideas on how to fix this?

updated

Code:

#include <stdio.h>
void swap (char*, char*);
bubble_string(char s[]);

int main(void)
{
    char *s= "xylophone";
    printf("%s", bubble_string(char *s));

    return 0;
}

bubble_string(char s[])
{
    char i, j, n;
    n  = strlen(s);
    for(i = 0; i < n - 1; ++i)
            for(j = n - 1; j > 0; --j)
                    if(s[j-1] > s[j])
                            swap(&s[j-1], &s[j]);
}

Author:,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/5683758/bubble-sort-program-c
Andrew Rasmussen :

Your bubble_string function needs to return a char*. Otherwise it is returning a void to printf, which is causing your error (because printf is expecting a char*).",
2011-04-16T01:07:14
Jim Lewis :

Among other problems, you declare that bubble_string does not return a value (by giving it return type void), then you go on to use it in the printf statement as if it returned a value. (At least that's how it looked before your edit...the way you have it now, it will default to returning an int, but you use it as if it were a string, and you're not actually returning anything from bubble_string.)\n\nAlso, your for loop syntax is way off. The outer loop should probably be\nmore like:\n\nfor(i=0; i < n-1; i++) {/* et cetera */}",
2011-04-16T01:08:35
yy