вам нужно использовать еще один getchar()
во внутреннем цикле while.
while (c != '\n') {
string[j][i] = c;
i++;
c = getchar(); /* this you need here to fetch char until \n encounters */
}
И нужно сделать переменную i
снова 0
, как только это string[j][i] = '\0';
будет сделано.
Также это
printf('string is: %s', string);
неправильно. Должно быть
printf("string is: %s", string); /* use double quotation instead of single */
Пример кода
int main(int argc, char *argv[]) {
char string[100][20];
int c, j = 0, i = 0;
while ((c = getchar()) != EOF) { /* this loop you need to terminate by pressing CTRL+D(in linux) & CTRL+Z(in windows) */
while (c != '\n') { /* this loop is for 1D array i.e storing char into each 1D array */
string[j][i] = c;
i++;
c = getchar(); /* add this, so that when you press ENTER, inner while loop fails */
}
string[j][i] = '\0';
j++;
i = 0;/* make it zero again, so that it put char into string[j][0] everytime once 1 line is completed */
}
for(int row = 0;row < j;row++) { /* rotate loop j times since string is
2D array */
printf("string is: %s", string[row]);
}
return 0;
}