+ 3

Why it don't reads the string s2 and print?

#include<stdio.h> #include<string.h> int main(){ int i=4; double d=4.0; char s[]="Hackerrank"; // Declare second integer, double, and String variables. int i2; double d2; char s2[100]; // Read and save an integer, double, and String to your variables. scanf("%d",&i2); scanf("%lf",&d2); gets(s2); // Print the sum of both integer variables on a new line. printf("\n%d",i+i2); // Print the sum of the double variables on a new line. printf("\n%.1lf",d+d2); // Concatenate and print the String variables on a new line strcat(s,s2); puts(s); // The 's' variable above should be printed first. return 0; }

8th Nov 2018, 3:31 PM
Naveen Kumar
Naveen Kumar - avatar
4 Antworten
+ 10
It's not printed because after the concatenation of the two strings, the concatenated string is stored in the first operand (in this case string s), so you need to print the 's' string.
8th Nov 2018, 5:21 PM
Nova
Nova - avatar
+ 9
And for the first string I.e. string s, you need to create a character array as char s can store only a single character. So the whole string is not stored. char s [] = {"Hackerrank"}; is correct way to declare it.
8th Nov 2018, 5:25 PM
Nova
Nova - avatar
+ 9
https://code.sololearn.com/c2H2kEDay2A0/?ref=app Use gets before the both the scanf(). This problem occured because the scanf () function reads a character and leaves a newline character in the memory buffer. So the gets () everytime read the newline character automatically, so it was not taking any input. You can refer the code below.
9th Nov 2018, 5:53 AM
Nova
Nova - avatar
0
I have fixed everything you said but still, the gets(s2) is not working i.e. I am not getting the option to enter the string. After entering i1 and d2 it directly prints the output.
9th Nov 2018, 3:05 AM
Naveen Kumar
Naveen Kumar - avatar