0

I am writing this exercise of bubble sort and I have to create 2 source files. The first contains the main code and the second contains my bubble sort algorithm. The main code includes the bubblesort.

Main:

#include<stdio.h>
#include<string.h>
#include"2.c"
int main(void)
{
  char text[100];
  int length;
  printf("Insert text: \n");
  gets(text);
  text=bsort(text);
  printf("String : %s  \n",text);
  return 0;
}

Bubblesort:

char *bsort(char text[100]){
    char temp[100];
    int length,i,j;
    length=strlen(text);
    for(i=1;i<length;i++){
            for(j=0;j<length-i;j++){
                if(text[j]>text[j+1]){
                        temp[0]=text_input[j];        //
                        text_input[j]=text_input[j+1];//Edit,had wrong code posted
                        text_input[j+1] =temp[0];     //
                 }
             }
      }
    return text;}

The thing is ,when i run it, i get a " incompatible types when assigning to type 'char[100]' from type 'char *'" error

I'm terribly new to c and I'm kinda lost, I'm browsing for an answer for quite some hours now. I understand that the function will return a char instead of char array(known as a string from java experience), but I just can't get to find a way to do this as hard as I try.

A solution would be great or just a small help would be appreciated.

EDIT: I forgot to mention what my program actually does. It is supposed to get a string from the user, apply the bubblesort algorithm which is is going to sort every character in the string alphabetically, i.e. "bagf" will be output as "abfg". Also added some comments

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Nick
  • 825
  • 1
  • 8
  • 20
  • Why do you write your code like that? are you a javascript programmer? and `temp[0], text[j]` does absolutely nothing. – Iharob Al Asimi Mar 09 '15 at 22:57
  • Try `char temp; ... temp = text[j]; text[j] = text[j+1]; text[j+1] = temp;` – chux - Reinstate Monica Mar 09 '15 at 22:58
  • 1
    @iharob: What is Javascript-like about the code? – Keith Thompson Mar 09 '15 at 22:58
  • 1
    Never use the `gets` function. It is inherently unsafe, and has been removed from the language as of the 2011 ISO C standard. Use `fgets` instead (which is a little trickier to use, since it can leave a newline `'\n'` in the array). – Keith Thompson Mar 09 '15 at 22:59
  • @KeithThompson it looks like javascript because it seems that the one who wrote it wanted to save every single characeter. – Iharob Al Asimi Mar 09 '15 at 23:00
  • possible duplicate of [How do I return a char array from a function?](http://stackoverflow.com/questions/5660527/how-do-i-return-a-char-array-from-a-function) – Samuel Edwin Ward Mar 09 '15 at 23:01
  • You can't return arrays in C. Typically you would pass the array (and its size) in to the function to modify it. Also, char arrays have a number of differences from Java Strings. – Samuel Edwin Ward Mar 09 '15 at 23:02
  • text serves as input/output parameter: it will be modified inside the function so you don't have to return it. Besides, you return a char to a pointer, which is not the same as char array, even while pointer aritmetic allows you to use a pointer as if it were a 1D array. So, just call bsort(text). Don't need to return anything. text will be sorted after returning from your function (assuming it is coded correctly -see iharob's answer- ) – mcleod_ideafix Mar 09 '15 at 23:07

1 Answers1

1

First please write your code in a way that it's readable, your problem is that you are not swapping any values

char *bsort(char text[100])
{
    char temp[100];
    int length, i, j;

    length = strlen(text);
    for(i = 1 ; i < length ; i++)
    {
        for (j = 0 ; j < length - i ; j++)
        {
            if(text[j] > text[j + 1])
            {
                temp[0]     = text[j];
                text[j]     = text[j+1];
                text[j + 1] = temp[0];
            }
         }
     }
     return text;
 }
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • And you don't need tem to be an array at all. Just a single char variable – mcleod_ideafix Mar 09 '15 at 23:04
  • Also replace this line: `text=bsort(text);` with just `bsort(text);`. `bsort` sorts the character array in place. Your implementation of bubblesort is utterly inefficient. – chqrlie Mar 10 '15 at 20:35