在C中将字符串读入二维数组

时间:2021-05-27 15:45:29

/* i want to read a number of strings from a text file (standard input) to a 2 dimensional Array using getchar(). please ignore the magic number in the code. please help me */

/ *我想使用getchar()从文本文件(标准输入)读取多个字符串到二维数组。请忽略代码中的幻数。请帮帮我 */

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

int
main(int argc, char *argv[]) {

    char string[100][20];
    int c, j = 0, i = 0; 

    while ((c = getchar()) != EOF) {    
        while (c != '\n') {
            string[j][i] = c;
            i++;
        }
        string[j][i] = '\0';
        j++;
    }
    printf('string is: %s', string);

    return 0;
}

1 个解决方案

#1


0  

you need to use one more getchar() in inner while loop.

你需要在内部while循环中再使用一个getchar()。

while (c != '\n') {
       string[j][i] = c;
       i++;
       c = getchar(); /* this you need here to fetch char until \n encounters */
 }

And need to make variable i again 0 once this string[j][i] = '\0'; is done.

一旦这个字符串[j] [i] ='\ 0',需要再次使变量i为0;已经完成了。

Also this

还有这个

printf('string is: %s', string);

is wrong. It should be

是错的。它应该是

printf("string is: %s", string); /* use double quotation instead of single */

Sample code

示例代码

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

#1


0  

you need to use one more getchar() in inner while loop.

你需要在内部while循环中再使用一个getchar()。

while (c != '\n') {
       string[j][i] = c;
       i++;
       c = getchar(); /* this you need here to fetch char until \n encounters */
 }

And need to make variable i again 0 once this string[j][i] = '\0'; is done.

一旦这个字符串[j] [i] ='\ 0',需要再次使变量i为0;已经完成了。

Also this

还有这个

printf('string is: %s', string);

is wrong. It should be

是错的。它应该是

printf("string is: %s", string); /* use double quotation instead of single */

Sample code

示例代码

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