C语言 如何输入多个字符串后跟换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22906049/
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 multiple strings followed by newline
提问by sunki08
this program asks about how many strings user wants to enter then it takes all strings and stores it.each string is followed by newline which signifies new string is being entered.
这个程序询问用户想要输入多少个字符串,然后它获取所有字符串并存储它。每个字符串后跟换行符,表示正在输入新字符串。
#include<stdio.h>
int main()
{
int n,i;
char str[20];
scanf("%d",&n); //how many string
//input each string
for(i=0;i<n;i++)
{
scanf("%s",&str[i]);
}
//display each string
for(i=0;i<n;i++)
{
printf("%s",str[i]);
}
return 0;
}
there are two problems i'm facing first whenever i compile and run it in devc++ after taking input of strings program crashes second i just want to know what i'm doing above is right ?
每当我在输入字符串程序崩溃后在 devc++ 中编译和运行它时,我首先面临两个问题,第二我只想知道我在上面做什么是对的?
回答by aspiring_sarge
What you want is a 2D character array, which is effectively an array of strings:
你想要的是一个二维字符数组,它实际上是一个字符串数组:
#include<stdio.h>
int main()
{
int n,i;
char str[20][20]; // Can store 20 strings, each of length 20
scanf("%d",&n); //how many string
//input each string
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
//display each string
for(i=0;i<n;i++)
{
printf("%s",str[i]);
printf("\n");
}
return 0;
}
The above code runs fine. The changes I have made to your original code are:
上面的代码运行良好。我对您的原始代码所做的更改是:
- Declared str as a 2D array of characters, which, as mentioned, is effectively an array of strings.
- Removed the & from before str[i]. An & is not needed when scanf is used to read in a string
- Added a new line character after each string is printed to make it look more like the input code
- 将 str 声明为一个二维字符数组,如前所述,它实际上是一个字符串数组。
- 删除了 str[i] 之前的 &。使用 scanf 读取字符串时不需要 &
- 在打印每个字符串后添加一个新行字符,使其看起来更像输入代码
回答by merlin2011
A char str[20]declares an array of 20characters not 20strings.
Achar str[20]声明一个20字符数组而不是20字符串。
This following loop in your code will write the first string starting at the start of str, and then write the second string starting at str + 1, which will overwrite the second and subsequent characters of the first string that you wrote.
代码中的以下循环将从 开始写入第一个字符串str,然后从 开始写入第二个字符串str + 1,这将覆盖您编写的第一个字符串的第二个和后续字符。
//input each string
for(i=0;i<n;i++)
{
scanf("%s",&str[i]);
}
The reason why your program segfaults is because, in the following line %srequires a string (char*) but you are passing it a char.
您的程序出现段错误的原因是,在以下行中%s需要一个字符串 ( char*) 但您将char.
printf("%s",str[i]);
Here is a fixed version of your code that should support as many strings as your system has memory for.
这是您的代码的固定版本,它应该支持与您的系统内存一样多的字符串。
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STRING_SIZE 1024
int main()
{
int n,i;
char ** str;
scanf("%d",&n); //how many string
// Allocate enough memory to store the number of strings requested
str = malloc(n * sizeof(char*));
for (i = 0; i < n; i++) {
str[i] = malloc((MAX_STRING_SIZE + 1) * sizeof(char));
memset(str[i], 0, MAX_STRING_SIZE + 1);
}
//input each string
for(i=0;i<n;i++)
{
scanf("%s", str[i]);
}
// display each string
for (i=0;i<n;i++)
{
printf("%s\n",str[i]);
}
// Free the memory we allocated
for (i = 0; i < n; i++)
free(str[i]);
free(str);
return 0;
}
回答by Jabberwocky
In your piece of code stris an array of 20 chars. But you need an array of 20 strings of say 100 chars.
在您的一段代码中str是一个包含 20 个字符的数组。但是你需要一个包含 20 个字符串的数组,比如 100 个字符。
If you write:
如果你写:
char str[20][100];
your program will work.
你的程序会起作用。
You can now enter at most 20 strings, each having a length of at most 100 characters terminating zero included.
您现在最多可以输入 20 个字符串,每个字符串的长度最多为 100 个字符,包括以零结尾。
回答by abhilb
A better approach will be to have the memory dynamically allocated.
更好的方法是动态分配内存。
#include <stdio.h>
int main()
{
int n,i;
char** str;
scanf("%d", &n);
str = (char**)malloc(sizeof(char*)*n);
for(i=0; i<n; i++)
{
str[i] = (char*)malloc(100); // assuming the strings will not be more than 100 chars long.
scanf("%s", &str[i]);
}
for(i=0; i<n; i++)
free(str[i]);
free(str);
}
回答by Zach P
I don't think you understand the concept of strings in C.
In C (and C++) a string is just an array of characters which ends with NULL (ASCII value - 0).
When you declared char str[20];you declared only one string with place for 19 characters (as NULL takes 1 character of the string). If you wanted to declare an array of strings you should have done something like this char strs[20][100];which will be a 2D array of characters - and in our case - 1D array of strings. It may contain 20 strings in the size of 99 characters + NULL or 100 strings at the size of 19 characters + NULL - depends the way you want it to be (usually you would treat it as 20 strings sized 99 + NULL).
The line scanf("%s", str[i]);is actually getting the input starting from the spot you mentioned in the string and ends it with null.
我认为您不了解 C 中字符串的概念。在 C(和 C++)中,字符串只是一个以 NULL(ASCII 值 - 0)结尾的字符数组。当您声明时,char str[20];您只声明了一个包含 19 个字符的字符串(因为 NULL 占用字符串的 1 个字符)。如果你想声明一个字符串数组,你应该做这样的事情char strs[20][100];,它将是一个二维字符数组 - 在我们的例子中 - 一维字符串数组。它可能包含大小为 99 个字符 + NULL 的 20 个字符串或大小为 19 个字符 + NULL 的 100 个字符串 - 取决于您想要的方式(通常您会将其视为 20 个大小为 99 + NULL 的字符串)。该行scanf("%s", str[i]);实际上是从您在字符串中提到的位置开始获取输入,并以空值结束。
char str[20];
scanf("%s", str);
(Notice - if it's the first spot - no need for & operator) with an input "banana" would end with a string with a NULL in the spot [6] (as strings and arrays go from 0 to the size declared-1). BUT:
(注意 - 如果它是第一个点 - 不需要 & 运算符)输入“香蕉”将在该点 [6] 中以一个带有 NULL 的字符串结尾(因为字符串和数组从 0 到声明的大小 - 1) . 但:
scanf("%s", &str[4]);
for example - the input would be starting from the 5th spot ([4]) and ending depends on the input. If you used this scanf then you'll probably have garbage values in str[0] to str[3] as you didn't do anything in it. Don't let the name confuse you - char str[20] - is only one string with the size of 19 characters + NULL. The correct way would be:
例如 - 输入将从第 5 个点 ([4]) 开始,结束取决于输入。如果你使用了这个 scanf,那么你可能会在 str[0] 到 str[3] 中有垃圾值,因为你没有在其中做任何事情。不要让这个名字混淆你 - char str[20] - 只是一个大小为 19 个字符 + NULL 的字符串。正确的方法是:
char str[20][20]; //20 strings with the size of 19 character + NULL
int i, n;
printf("Enter the number of strings you want to enter: ");
scanf("%d", &n);
for(i = 0 ; i < n ; i++)
{
scanf("%s", &str[i]);
}
and that's it. I hope I helped :)
就是这样。我希望我有所帮助:)

