简体   繁体   English

程序在接受C中的输入中的第二个字符串之前运行

[英]Program Runs before accepting second string in input in C

I am trying to run the following code but the program accepts only one string and displays the output immediately without waiting for the second string to be entered. 我试图运行以下代码但程序只接受一个字符串并立即显示输出而不等待输入第二个字符串。 The program is for 2 string concatenation. 该程序用于2字符串连接。 Here is the code :- 这是代码: -

#include <stdio.h>

main()
{
    int i, j, len=0;
    char name[100], abc[100];

    printf("\nPlease Enter String 1 =\t");
    scanf("%[^\n]c",&name);
    printf("\nPlease Enter String 2 =\t");
    scanf("%[^\n]c",&abc);

    for(i=0; name[i]!='\0'; i++)
        len++;
    for(j=0; abc[j]!='\0'; j++)
    {
        len++;
        name[len]=abc[j];
    }

    printf("\nThe Concatenated String Is =\t");
    puts(name);
}

Use the following scanf instead: 请改用以下scanf

scanf("%[^\n]",name);
.....
scanf(" %[^\n]",abc);

Please refer to this scanf() manual page for more detail of how to use scanf() . 有关如何使用scanf()更多详细信息,请参阅此scanf()手册页。

Use fgets instead of scanf, also you were incrementing len at the wrong place: 使用fgets而不是scanf,你也在错误的位置增加len:

#include <stdio.h>
#include <string.h>
int main() {
    int len = 0;
    char name[100], abc[100];
    printf("\nPlease Enter String 1 =\t");
    fgets(name, 100, stdin);
    len = strlen(name) - 1;
    name[len] = 0;
    printf("\nPlease Enter String 2 =\t");
    fgets(abc, 100, stdin);
    abc[strlen(abc) - 1] = 0;
    strcpy(name+len, abc);
    printf("\nThe Concatenated String Is =\t");
    puts(name);
    return 0;
}

You have to throw away the newline character ('\\n'). 你必须扔掉换行符('\\ n')。

Try: 尝试:

while(getchar() != '\n')
    continue;

after each scanf 每次scanf

I would like to suggest you not to use scanf . 我想建议你不要使用scanf Better to use fgets instead. 最好使用fgets
By the way the reason that your program accepts only one string and displays the output immediately without waiting for the second string to be entered is the \\n character left behind by the first scanf after pressing the Enter key. 顺便说一下,程序只接受一个字符串并立即显示输出而不等待第二个字符串输入的原因是按下Enter键后第一个scanf留下的\\n字符。 To eat up this newline character you may use gatchar() after first scanf . 吃了这个换行符您可以使用gatchar()后首先scanf

printf("\nPlease Enter String 1 =\t");
scanf("%[^\n]c",name);
getchar();
printf("\nPlease Enter String 2 =\t");
scanf("%[^\n]c",abc);  

Other two mistakes are: 其他两个错误是:
1. Wrong increment of len 1.错误增量len

for(i=0; name[i]!='\0'; i++)
    len++;
for(j=0; abc[j]!='\0'; j++)
{
    name[len++]=abc[j];  
}   

2. Reason for weird output is the string is not NUL terminated. 2.奇怪输出的原因是字符串未终止NUL。 Add this line after second for loop. 在第二个for循环后添加此行。

name[len] = '\0'; // add this to null terminate your string.  

Here is your working Code 这是你的工作代码

should be 应该

scanf("%[^\n]%*c", name);
printf("\nPlease Enter String 2 =\t");
scanf("%[^\n]%*c", abc);
for(i=0;name[i]!='\0';i++)
    len++;
for(j=0;abc[j]!='\0';j++){
    name[len]=abc[j];
    len++;
}
name[len]='\0';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM