hi..i try to implement the bubble sorting where it read from file..i mean line by line then break into token..it can break into token but it can't sort the words..
Eg. input
output from the program.
output that i want..
it seems like it din't pass any value into the bubbleSort..my bubble sort function can work with character by character but i jus want it works by words by words..can give some tips where i did wrong in my program??..thanks..
here is the program:
Code:
/* header file */
#include <stdio.h>
#include <stdlib.h>
typedef char* Item;
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "b.h"
#define M 50
int main()
{
FILE *infile, *outfile;
char line[1000], words[1000];
char *token;
char temp[1000];
char *filename = "test.txt";
char *delimeter = " ";
if((infile = fopen(filename, "r")) == NULL)
{
printf("Error");
}
if (fgets(line, sizeof(line), infile) != NULL)
{
printf("%s\n", strtok(line, " "));
while ( (token=strtok(NULL, " ")) != NULL)
{
bubble(&token, M);
printf("%s\n", token);
}
}
return 0;
}
void bubble(Item a[], int N)
{
int pass, pos;
Item temp;
for (pass = 1; pass < N; pass ++)
for (pass = N - 1; pos >= pass; pos --)
{
if (a[pos] < a[pos-1])
{
*temp = *a[pos];
*a[pos] = *a[pos-1];
*a[pos-1] = *temp;
}
}
}