- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一段小代码来获取多个字符串作为用户的输入。在此之前,我要求用户输入要作为输入的字符串数量。当以字符串作为输入时,换行符会自动占据第一个字符串的位置。我不知道发生了什么:(这是我的代码:
#include<stdio.h>
#include<string.h>
void main()
{
char *arr[20],str[40];
int n;
printf("enter the number of strings\n");
scanf("%d",&n);
int i;
printf("Enter the strings\n");[It isn't taking 3 strings as input,as a newline character is already occupying first place][1]
for(i=0;i<n;i++)
{
gets(str);
arr[i]=(char *)malloc(sizeof str);
strcpy(arr[i],str);
}
printf("The Strings are:\n");
for(i=0;i<n;i++)
{
printf(arr[i]);
printf("\n");
}
}
最佳答案
根据评论,您的代码中存在许多问题。首先获取
,不要使用它,这是一种“悬而未决”的进攻——说得够多了。
接下来,验证所有用户输入。据您所知,猫可能会踩键盘。确保测试收到的输入内容,并确保它符合您的预期。
将 scanf
与面向行的输入(例如 fgets
或 getline
)混合可能会给新用户带来问题。为什么?scanf
系列函数不会删除 '\n'
而是将其保留在输入缓冲区中(例如 stdin
)。然后,您尝试使用 fgets
读取它在 stdin
中看到的第一个字符是什么?'\n'
,它读取并认为是一个整行。如果您要使用 scanf
读取字符串的数量,则需要删除 scanf
的 '\n'
code> 留在 stdin
中。
这并不难,实际上您可以在格式字符串中使用 scanf
提供的赋值抑制运算符。例如:
scanf ("%d%*c", &n);
*
是赋值抑制运算符,当与 %*c
一起使用时,它只是告诉 scanf
读取并丢弃/em> 下一个字符不添加到匹配计数
(例如scanf
返回的内容)。
在填充数组时,您将需要使用 while
循环而不是 for
循环(或使用独立索引)。为什么?如果用户使用ctrl + d(或windoze上的ctrl + z)取消输入会怎么样。如果无论用户做什么都迭代到 n,您可以轻松地尝试索引尚未分配的数组元素,或分配未输入的字符串。
总而言之,您可以执行以下操作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { MAXA = 20, MAXS = 40 }; /* max ptrs and str */
int main (void) {
char *arr[MAXA] = {NULL}, str[MAXS] = "";
int i = 0, n = 0, ndx = 0; /* initialize all variables */
printf ("enter the number of strings: ");
/* use assignment suppression %*c to discard the \n */
if (scanf ("%d%*c", &n) != 1) { /* always validate input */
fprintf (stderr, "error: invalid integer input.\n");
return 1;
}
if (n > MAXA) { /* validate the value of n */
fprintf (stderr, "warning: n > %d, using %d as limit.\n",
MAXA, MAXA);
n = MAXA;
}
printf("Enter the strings\n");
while (ndx < n && fgets (str, sizeof str, stdin)) { /* validate input */
size_t len = strlen (str); /* get the length */
if (str[len - 1] == '\n') /* check for '\n' */
str[--len] = 0; /* overwrite with nul-terminator */
if (!(arr[ndx] = malloc (len + 1))) { /* validate allocation */
fprintf (stderr, "error: virtual memory exhausted.\n");
break;
}
strcpy (arr[ndx], str); /* copy to array */
ndx++; /* increment index */
}
printf("\nThe Strings are:\n");
for (i = 0; i < ndx; i++) { /* you have ndx strings not n */
printf (" arr[%2d] : %s\n", i, arr[i]);
free (arr[i]); /* free memory when no longer needed */
}
return 0;
}
示例使用/输出
$ ./bin/nogets
enter the number of strings: 3
Enter the strings
My dog
has a lot
of fleas.
The Strings are:
arr[ 0] : My dog
arr[ 1] : has a lot
arr[ 2] : of fleas.
现在尝试同样的操作,然后按ctrl + d,而不是输入“of fleas。”
(您已被覆盖)。
最后,在您编写的动态分配内存的任何代码中,对于分配的任何内存块,您都有 2 个责任:(1) 始终保留指向起始地址的指针> 对于内存块,(2) 当不再需要它时可以释放。养成跟踪分配的内存并释放
它的习惯,而不是依赖它在退出
时完成。当您的程序变得更加复杂时,这将对您很有帮助。
查看代码,确保您了解发生了什么。如果您有任何疑问,请告诉我。
关于c - 将多个字符串作为输入时出现不必要的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40837206/
我有一个带有 的表格我想在服务器端捕获该文本区域中的任何换行符,并将它们替换为 . 这可能吗? 我尝试设置 white-space:pre在 textarea 上的 CSS,但仍然不够。 最佳答案
我正在通过 FileStream 更改文件(这是一个非常大的文件,我只需要更改标题而不重写整个文件。 该文件可以有 Unix 或 Windows 换行符,知道哪一个对我来说很重要,这样我可以在更新文件
如何以编程方式(不使用 vi)将 DOS/Windows 换行符转换为 Unix 换行符? dos2unix 和 unix2dos 命令在某些系统上不可用。 如何使用 sed、awk 和 tr 等命令
我编辑了一个 BibteX 文件,到目前为止,它显示了我需要的所有信息。 FUNCTION {electronic} { output.bibitem format.btitle "title"
您的软件是否处理来自其他系统的换行符? Linux/BSD linefeed ^J 10 x0A Windows/IBM return linefeed ^M^
我发现 Windows 命令行重定向会自动将 '\n' 替换为 '\r\n'。有什么方法可以避免这种情况?因为在 stdout 或 stderr 重定向之后,如果您将 '\r\n' 写入控制台,您将得
来自 this question ,这个…… lines = foo.value.split(/\r\n|\r|\n/); 是拆分字符串的一种方法,但如何用换行符将其连接回去? 另外,我想知道如果我说
我正在尝试获取新行,但如果我使用 \n 它不起作用。 任何通过向字符串添加一些东西来换行的方法,例如 \r\n (这也不起作用) gfx.DrawString("Project No \n" + te
我有一串数据,中间有换行符。例如: "Product Name \n Product Color \n Product Quantity \n Product Location \n Product
我正在尝试让 scmCommentPrefix 按照 http://maven.apache.org/plugins/maven-release-plugin/faq.html#scmCommentP
如何检查正则表达式 /^\n/在字符串中 blahblahblah 我似乎无法从 php 的 preg_match 获得返回值 1 . 编辑: 由于某种原因,我的坏处是 CR 本身就是我的换行符。
我很难在与文本字符串相同的行上输出变量。当我使用Write-Host而不是Write-Output时,它可以工作。我想使用Write-Output,因为这似乎是最佳做法(将内容保留在管道中),但是Wr
我正在Powershell中工作,以为here字符串中的特定单词着色。除包含回车/换行符的单词外,其他功能均有效。没有这些字符,如何计算单词的长度? 以下是我正在使用的功能和测试数据。我希望第二行上的
我有一个在Powershell中运行的脚本,并且我希望能够在脚本名称和脚本内容本身之间的结果文本文件输出中添加一行。 当前,从下面开始,行$str_msg = $file,[System.IO.Fil
有人知道smalltalk中字符串的换行符是什么吗? 我试图将字符串拆分为单独的行,但我无法弄清楚smalltalk 中的换行符是什么。 即。 string := 'smalltalk is
我有以下 printf 语句: printf ("%s (%s)\n",$row["word"], $row["definition"]); 我正在尝试解决换行符而不是: word defin
这个问题已经有答案了: how to use dotall flag for regex.exec() (4 个回答) 已关闭 7 年前。 字符串内容
我想用 CSS 换行。我正在使用内容。 td:before { content: "Test\A Test2"; } 它不工作。如何正确
考虑一个具有 UNIX 行结尾的 C++ 文件(即 '\x0a' 而不是 "\x0d\x0a")并包含以下原始字符串文字: const char foo[] = R"(hello^M )"; (其中
我有以下 printf 语句: printf ("%s (%s)\n",$row["word"], $row["definition"]); 我正在尝试解决换行符而不是: word defin
我是一名优秀的程序员,十分优秀!