Search code examples
carraysstringpointersbubble-sort

Bubblesort list of words alphabetically. I have an array of pointers pointing to each word


So I have a program that takes in a list and puts them into an array of pointers pointing to each word. So far if i just print the lines part of my program with the get_lines function it prints the .txt file perfectly. I just need help to bubblesort these words alphabetically but im not sure how. Please help!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int readfile(FILE*fp,char**cbuf);
char**get_lines(char *cbuf, int bufsize, int num_word);
int readword(FILE*tmp);
void sortA(char lines,int bufsize,int num_word);

int main(int argc, char*argv[])
{
  int i, bufsize, num_word;
  char*cbuf;
  char**lines;
  FILE*fp;
  FILE*tmp;
  FILE*outfile;
  char*newlines;

  outfile = fopen("words2.txt","w");
  if((fp=fopen(argv[1],"r"))== NULL)
    {perror("ERROR: bad/no filename");
      exit(0);
    }
  tmp = fopen(argv[1],"r"); 
  bufsize = readfile(fp,&cbuf);
  num_word = readword(tmp);
  lines = get_lines(cbuf,bufsize,num_word);
  i = 0;
  while(newlines[i] != NULL)
    {
      fprintf(outfile,"%s\n",newlines[i]);
      i++;
      }
  return 0;
}

int readfile(FILE*fp, char**cbuf)
{
  int i;
  char c;
  fseek(fp, 0L, SEEK_END);
  int bufsize = ftell(fp);
  fseek(fp, 0L, SEEK_SET);

  *cbuf = (char *)malloc(sizeof(char) * bufsize +1);
  for (i = 0; i < bufsize; i++)
    {
      c = fgetc(fp);
      (*cbuf)[i] = c;
    }
  return bufsize;
}

int readword(FILE*tmp)
{
  int word = 0;
  char c;

  while((c = fgetc(tmp)) != EOF )
    {
      if (c == '\n')
    word++;
    }
    return word;
}

char**get_lines(char*cbuf, int bufsize, int num_lines)
{
  char **lines = malloc((num_lines + 1) * sizeof *lines);

  int line = 0;
  while (line < num_lines)
    {
      lines[line++] = cbuf;
      cbuf = strchr(cbuf,'\n');

      if (!cbuf)
    break;

      *cbuf++ = '\0';
    }
  lines[line] = NULL;

  return lines;
}

void SortA(char lines, int bufsize, int num_word)
{
  char t[bufsize];
  int i,j;


  for (i = 1; i < num_word; i++)
    {
      for (j = 1; j < num_word; j++)
    {
      if (strcmp(lines[j - 1], lines[j]) > 0)
        {
          strcpy(t, lines[j - 1]);
          strcpy(lines[j - 1], lines[j]);
          strcpy(lines[j],t);
        }
      }
    }

Solution

  • I found two mistakes in your code. The first is not checking argc before using argv[1]. The other is using newlines which was uninitialised, instead of lines.

    I also rewrote SortA() and gave it fewer arguments. I corrected the loops, and instead of swapping the strings, I swap the pointers. You can't swap the strings - they don't take the same number of bytes, and they sit sequentially in your buffer.

    ...
    void SortA(char **lines, int num_word);
    
    int main(int argc, char*argv[])
    {
        ...
        SortA(lines, num_word);
        i = 0;
        while(lines[i] != NULL)
        {
            fprintf(outfile,"%s\n",lines[i]);
            i++;
        }
        return 0;
    }
    
    void SortA(char **lines, int num_word)
    {
        char *w;
        int i,j;
        for (i=0; i<num_word-1; i++)
        {
            for (j=i+1; j<num_word; j++)
            {
                if (strcmp(lines[i], lines[j]) > 0)
                {   
                    w = lines[i];
                    lines[i] = lines [j];
                    lines[j] = w;
                }
            }
        }
    }
    

    Input file:

    one
    two
    three
    four
    five
    six
    seven
    eight
    nine
    ten
    

    Output file:

    eight
    five
    four
    nine
    one
    seven
    six
    ten
    three
    two