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;
}