C语言 如何在C中的二维数组中输入字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14536139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to input a string in a 2d array in C?
提问by Pranav Sharma
If i want to take an input in a 2d array with each string in one row, and the next in the other(i.e. change the row on pressing enter). How can i do that in C. C doesnt seem to have convenient "String" Handling. I obviously mean doing so without the use of getchar().
如果我想在二维数组中输入一个输入,其中每个字符串在一行中,下一个在另一行中(即按回车键更改行)。我怎么能在 C 中做到这一点。 C 似乎没有方便的“字符串”处理。我的意思显然是在不使用 getchar() 的情况下这样做。
回答by Brijesh Gajjar
#include<stdio.h>
main()
{
char student_name[5][25];
int i;
for(i=0;i<5;i++)
{
printf("\nEnter a string %d: ",i+1);
scanf(" %[^\n]",student_name[i]);
}
}
u can read strings using 2d array without using getchar() by putting space in scanf(" %[^\n]") ; before %[^\n]!
通过在 scanf(" %[^\n]") 中放置空格,您可以使用二维数组读取字符串,而无需使用 getchar() ;在 %[^\n]!
回答by rashok
3 ways are there which are mentioned below.
有3种方法,下面提到。
If you know the maximum number of strings and maximum number of chars, then you can use the below way to declare a 2D character array.
如果您知道最大字符串数和最大字符数,那么您可以使用以下方式声明一个二维字符数组。
char strs[MAX_NO_OF_STRS][MAX_NO_CHARS] = {0};
for (i = 0; i < MAX_NO_OF_STRS; i++)
{
scanf("%s", strs[i]);
}
If you know the maximum number of strings, and you dont want to waste the memory by allocating memory for MAX_NO_CHARSfor all strings. then go for array of char pointers.
如果您知道最大字符串数,并且不想通过MAX_NO_CHARS为所有字符串分配内存来浪费内存。然后去寻找字符指针数组。
char temp[MAX_NO_CHARS] = {0};
char *strs[MAX_NO_OF_STRS] = NULL;
for (i = 0; i < MAX_NO_OF_STRS; i++)
{
scanf("%s", temp);
strs[i] = strdup(temp);
}
If you know the maximum number of strings during run time means, you can declare a double pointer of char. Get the number of strings nfrom user and then allocate memory dynamically.
如果您知道运行时字符串的最大数量意味着,您可以声明一个双指针char. n从用户那里获取字符串的数量,然后动态分配内存。
char temp[MAX_NO_CHARS] = {0};
char **strs = NULL;
int n = 0;
scanf("%d", &n);
strs = malloc(sizeof(char*) * n);
for (i = 0; i < n; i++)
{
scanf("%s", temp);
strs[i] = strdup(temp);
}
回答by StoryTeller - Unslander Monica
An alternative to using mallocand filling up an array of pointers with buffers of a fixed size, would be to allocate a 2d array (in static storage or on the stack) and fill it up. KingsIndian modifed code example would than look like this:
使用malloc固定大小的缓冲区并填充指针数组的另一种方法是分配一个二维数组(在静态存储中或在堆栈上)并填充它。KingsIndian 修改后的代码示例如下所示:
#include <stdio.h>
int main()
{
char str[2][256] = {{0}};
int i = 0;
for(i=0;i<2;i++)
{
scanf("%255s", &str[i][0]);
}
return 0;
}
If all strings you expect to get are no longer than some size, than this approach will spare you the need to deal with freeing the memory yourself. It is less flexible however, meaning that you can't fit the size of an individual buffer to the string it contains.
如果您希望获得的所有字符串都不超过某个大小,那么这种方法将使您无需自己处理释放内存。然而,它不太灵活,这意味着您无法将单个缓冲区的大小适合它包含的字符串。
EDIT
编辑
Adding to the information in the comment, to read a string that is terminated only by a new-line, rather than by any whitespace:
添加到注释中的信息,读取仅由换行符而不是任何空格终止的字符串:
scanf("%255[^\n]", str[i]);

