0

I have a program to reverse words. Problem is when i enter an empty line(just press enter), on the output there is missing word(that is after empty line). What should i make in my code?

eg:

input:

  • aaaa
  • zzzz
  • cccc
  • ffff

output:

  • aaaa
  • cccc
  • zzzz

"ffff" is missing:(

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

#define N_MAX 100000

int compare(const void *a, const void *b)   /* funkcja uzywana przy qsort */
{
    return strcmp(*((char**) a), *((char**) b));
}


int main()
{
    int i=0, j;
    char* Tekst[N_MAX];

    char c;
    while ((c = getchar()) != EOF)
    {
        char tab1[1000]={0};    
        char tab2[1000]={0}; 

        tab1[0] = c;
        gets(tab2);
        strcat(tab1, tab2);

        if (c!='\n')
        {                      
            Tekst[i] = (char*)malloc((strlen(tab1)+1)*sizeof(char));
            strcpy(Tekst[i], tab1);
            i++;
        }
    }

    qsort(Tekst, i, sizeof(char *), compare); 

    puts ("\n\n");
    for (j=0; j<i; j++)
    {
        puts(Tekst[j]);
    }
    return 0;
}
ilim
  • 4,477
  • 7
  • 27
  • 46
mmm
  • 260
  • 4
  • 21
  • Please show your research/debugging effort so far. Please read [Ask] page first. – Sourav Ghosh Jan 19 '17 at 12:30
  • 2
    Careful, [`getchar`](http://en.cppreference.com/w/c/io/getchar) returns an `int`! – Some programmer dude Jan 19 '17 at 12:30
  • 2
    Even more careful: Don't use `gets`! Actually, I don't see why you simply don't use `fgets` to begin with? Why are you needing the `getchar` and the `gets`? That will make your code overly complicated. – Some programmer dude Jan 19 '17 at 12:32
  • Lastly, even though it's not a standard function, I know of no system which doesn't have the `strdup` function which does what you do with your `malloc` and `strcpy` calls. – Some programmer dude Jan 19 '17 at 12:37
  • Please write in pseudo code the struct of your program, and only then translate it in C. And please stop using `gets`... – Serge Ballesta Jan 19 '17 at 12:41
  • @Someprogrammerdude i want to dont use 'gets' but its project for my studies and teacher requires to use 'gets' anyway i'll read your advices and try to figure it how to make this code better thanks – mmm Jan 19 '17 at 12:42
  • @MadMike6661 maybe your teacher should [read this](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – Jabberwocky Jan 19 '17 at 12:43
  • Sounds like your teacher needs to take some C programming classes. – Lundin Jan 19 '17 at 12:47
  • @Lundin maybe he does but i can't argue with him :/ – mmm Jan 19 '17 at 12:50
  • What is your program supposed to output for an input of `def`[Enter]`abc` ? Your program looks as it sorts words, there is no "reversing". – Jabberwocky Jan 19 '17 at 12:59

1 Answers1

1

You don't need getchar() and strcat here.

You probably want this:

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

#define N_MAX 100000

int compare(const void *a, const void *b)   /* funkcja uzywana przy qsort */
{
  return strcmp(*((char**)a), *((char**)b));
}

int main()
{
  int i = 0, j;
  char* Tekst[N_MAX];

  while (1)
  {
    char tab2[1000] = { 0 };

    if (fgets(tab2, 1000, stdin) == NULL)
      break;   // on EOF fgets returns NULL

    tab2[strlen(tab2) - 1] = 0;   // get rid of \n at the end of the string

    Tekst[i] = (char*)malloc((strlen(tab2) + 1) * sizeof(char));
    strcpy(Tekst[i], tab2);
    i++;
  }

  qsort(Tekst, i, sizeof(char *), compare);

  puts("\n\n");
  for (j = 0; j<i; j++)
  {
    puts(Tekst[j]);
  }
  return 0;
}

If you really want to use gets instead of `fgets, replace

if (fgets(tab2, 1000, stdin) == NULL)

by

if (gets(tab2) == NULL)
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115