I want to store the words of a text in a two dimensional character array, where identical words are stored exactly once. The problem is that program seems to not store the last few words. I have included the whole program, because I can't figure out the problematic part. Here's my try:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define N 20
#define M 110
int main() {
char text[M], c[N][N], word[N];
int i, j, k=0, l=0, d, v;
for(i=0; i<N; i++) {
for(j=0; j<N; j++) {
c[i][j]='\0';
}
}
for(i=0; i<M; i++) {
text[i]='\0';
}
for(i=0; i<N; i++) {
word[i]='\0';
}
for(i=0; i<M; i++) {
d=scanf("%c", &text[i]);
if(d==EOF) {
text[i]='\0';
break;
}
}
for(i=0; i<strlen(text); i++) {
if(isspace(text[i])==0) {
word[l]=tolower(text[i]);
l++;
}
if((i==strlen(text)-1)||((isspace(text[i])!=0)&&(isspace(text[i+1])==0))) {
for(v=0; v<i; v++) {
for(j=0; j<N; j++) {
if(word[j]!=c[v][j]) {
break;
}
}
if(j==N) {
l=0;
break;
}
}
if(v==i) {
for(j=0; j<l; j++) {
c[k][j]=word[j];
}
k++;
l=0;
}
}
}
printf("\n\n");
for(i=0; i<N; i++) {
for(j=0; j<N; j++) {
printf("%c", c[i][j]);
}
printf(" ");
}
return 0;
}
For some input not all words get printed (not stored?).
For example with input: a b c d e f g h i k l m n o p
and Ctrl+D
the output is: a b c d e f g h i k
I noticed that if I increase N
and give the same input, the number of words printed also increases. Any help will be much appreciated.
EDIT:
if we replace
for(v=0; v<i; v++)
for(j=0; j<N; j++)
//...
if(j==N)
//...
if(v==i)
with
for(v=0; v<k; v++)
for(j=0; j<l; j++)
//...
if(j==l)
//...
if(v==k)
the program works properly.