#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int t;
char str[100];
scanf("%d",&t);
while(t–)
{
scanf("%[^/n]%*c",str);
printf("%s",str);
}
}
This code is not giving desired output . I must be doing some stupid mistake please help
Use scanf statement as follows:
scanf("%[^\n]s",str);
you can try taking input by-
fgets(str,100,stdin);
see if it works…
Change your code body to this-
char c;
while(t--)
{
scanf("%c",&c);
scanf("%[^\n]s",str);
printf("%s\n",str);
//cout<<"Str is "<<str<<endl;
}
This should work for you ^^
Reason for your error-
The input can be re-written as-
2\n
String 1\n
String 2\n
scanf("%[^\n]s",str);
will read til it encounters a \n character . But it will NOT read this “\n” character. So it got stuck on the “\n” in first line itself, and thus str remained an empty string till end of test cases.
The %c char will “catch” the newline character so scanf reads the next string.
Also, your newline character in the code is wrong. Its “\n” and NOT “/n”
try this:
scanf ("%[^\n]%*c", str);
#include
#include
using namespace std;
int main()
{
int t;
const int MAX=250;
char str[MAX];
cin>>t;
for(;t>0;t–)
{
cin.get();
cin.get(str,MAX);
printf("%s\n",str);//cout<<str;
}
return 0;
}
still not working
Yes, you are right.
It is not working in codechef online ide.
working on codeblocks
header file reqd?
You can use gets() function instead.
It can receive a multi-word string.
gets example
#include <stdio.h>
int main()
{
char string [256];
printf ("Insert your full address: ");
gets (string);
printf ("Your address is: %s\n",string);
return 0;
}
header file reqd?
not working anyways
when you are inputting the test cases give a space -
scanf("%d ",&t);
It will work fine…
yes, it is also not working.
I am surprised to know that too.
try this-
scanf("%d ",&t);
while(t–)
{
char str[101];
fgets(str,100,stdin);
printf("%s",str);
}
hope it works…
#include int main() { int t; char str[100]; scanf("%d",&t); while(t--) { scanf(" %[^\n]s",str); printf("%s",str); } }
You can use this.
It is working.
u cant use gets with c++(gcc 4.9.2) or c++14. try with gcc(4.3.2).it might work
it worked but I have a doubt that when for the first time the while loop will execute the scanf statement contaning char c will be executed and the string I will input would be for that statement right but what is happening is that variable str is catching that string and c variable is not getting any input . please explain which \n c variable is catching