scanf fails to read a string in C [duplicate] - c

This question already has answers here:
Reading string from input with space character? [duplicate]
(13 answers)
Closed 4 years ago.
I have a simple C program as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100],b[100];
char *ret;
printf("Enter the string\n");
scanf("%s",a);
printf("Enter the substring to be searched\n");
scanf("%s",b);
ret= strstr(a,b);
if(ret==NULL)
{
printf("Substring not found\n");
}
else
{
printf("Substring found \n");
}
}
When I execute the following program, scanf to read the string into b is not waiting for me to enter the substring and the print statement that prints the substring not found is being printed on the console. I tried to give %sand tried in the scanf statement and removed \n from the printf statements and nothing changed the way it executed the program. It would be great if someone solves this simple problem. Thanks in advance.

You can use scanf ("%[^\n]%*c", variable); with this scanf will read the whole line, instead of stopping when a space is reached.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100];
char b[100];
char *ret;
printf("Enter the string\n");
scanf ("%[^\n]%*c", a);
printf("Enter the substring to be searched\n");
scanf ("%[^\n]%*c", b);
ret= strstr(a,b);
if(ret==NULL)
{
printf("Substring not found\n");
}
else
{
printf("Substring found \n");
}
}
Also you can use fgets
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100];
char b[100];
char *ret;
printf("Enter the string\n");
fgets(a,100,stdin);//100 is the size of the string, you could use sizeof()
printf("Enter the substring to be searched\n");
fgets(b,100,stdin);//100 is the size of the string, you could use sizeof()
ret= strstr(a,b);
if(ret==NULL)
{
printf("Substring not found\n");
}
else
{
printf("Substring found \n");
}
}

try to use fgets instead of scanf, probably the reason is that the spaces are treated as delimiters, and the parts before the space are treated as a and the part right after the space will be treated as b. Therefore the programme did not prompt you for another input.
For your information: Reading string from input with space character?

Related

When I try to print the string inputted by the user in the console and then print the same inputted string;the first word gets omittedl. How do i fix?

As you can see in the above mentioned title , I don't know if there is something I am doing wrong . I have just started learning c . Please help me with the code.
#include <stdio.h> //code in c language.
#include <string.h>
int main()
{
char str[20];
printf("Enter the string:");
scanf("%s",&str);
gets(str);
printf("The string is: %s \n",str);
printf("The reverse string is : %s",strrev(str));
return 0;
}
In order to get the complete string from stdin, its recommended to use fgets like follows:
#include <stdio.h> //code in c language.
#include <string.h>
int main()
{
char str[20];
printf("Enter the string:");
/* strtok removes trailing newline character from fgets() */
strtok(fgets(str, 20, stdin), "\n");
printf("The string is: %s \n", str);
/* error is still present here */
printf("The reverse string is : %s", strrev(str));
return 0;
}
The above should answer your question although strrev() is currently causing an error!
Here is a link to a previous stackOverflow question on successfully reversing a string: Reversing a string in C

Cannot understand how loop is running here [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years ago.
I want to get rid of buffer overflow and I am using fgets instead of scanf to limit the characters. But whenever I enter something with scanf before fgets, it doesn't work correctly anymore.
This code works correctly
#include <stdio.h>
int main()
{
char name[10];
printf("Who are you? \n");
fgets(name,10,stdin);
printf("Good to meet you, %s.\n",name);
return(0);
}
This code does not read name correctly
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc();
int view_list();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, choice;
int new_account, list;
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
scanf("%d",&choice);
if (choice==one){new_account = new_acc();}
return 0;
}
int new_acc(){
char name[MAX], address, account;
printf("Enter your name: ");
fgets(name, MAX, stdin); /* it is the code */
}
Why is it happening and how do I fix it?
Don't mix scanf() with fgets() in the same code.
scanf("%d",&choice); does not consume all the line - it leaves the trailing '\n' for the later fgets() to consume as an empty line.
scanf() is difficult to use in a secure fashion.

Fgets don't let me input in a loop [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years ago.
I want to get rid of buffer overflow and I am using fgets instead of scanf to limit the characters. But whenever I enter something with scanf before fgets, it doesn't work correctly anymore.
This code works correctly
#include <stdio.h>
int main()
{
char name[10];
printf("Who are you? \n");
fgets(name,10,stdin);
printf("Good to meet you, %s.\n",name);
return(0);
}
This code does not read name correctly
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc();
int view_list();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, choice;
int new_account, list;
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
scanf("%d",&choice);
if (choice==one){new_account = new_acc();}
return 0;
}
int new_acc(){
char name[MAX], address, account;
printf("Enter your name: ");
fgets(name, MAX, stdin); /* it is the code */
}
Why is it happening and how do I fix it?
Don't mix scanf() with fgets() in the same code.
scanf("%d",&choice); does not consume all the line - it leaves the trailing '\n' for the later fgets() to consume as an empty line.
scanf() is difficult to use in a secure fashion.

Fgets not working properly and something unusual happens with it [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years ago.
I want to get rid of buffer overflow and I am using fgets instead of scanf to limit the characters. But whenever I enter something with scanf before fgets, it doesn't work correctly anymore.
This code works correctly
#include <stdio.h>
int main()
{
char name[10];
printf("Who are you? \n");
fgets(name,10,stdin);
printf("Good to meet you, %s.\n",name);
return(0);
}
This code does not read name correctly
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc();
int view_list();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, choice;
int new_account, list;
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
scanf("%d",&choice);
if (choice==one){new_account = new_acc();}
return 0;
}
int new_acc(){
char name[MAX], address, account;
printf("Enter your name: ");
fgets(name, MAX, stdin); /* it is the code */
}
Why is it happening and how do I fix it?
Don't mix scanf() with fgets() in the same code.
scanf("%d",&choice); does not consume all the line - it leaves the trailing '\n' for the later fgets() to consume as an empty line.
scanf() is difficult to use in a secure fashion.

Using fgets after scanf [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years ago.
I want to get rid of buffer overflow and I am using fgets instead of scanf to limit the characters. But whenever I enter something with scanf before fgets, it doesn't work correctly anymore.
This code works correctly
#include <stdio.h>
int main()
{
char name[10];
printf("Who are you? \n");
fgets(name,10,stdin);
printf("Good to meet you, %s.\n",name);
return(0);
}
This code does not read name correctly
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc();
int view_list();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, choice;
int new_account, list;
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
scanf("%d",&choice);
if (choice==one){new_account = new_acc();}
return 0;
}
int new_acc(){
char name[MAX], address, account;
printf("Enter your name: ");
fgets(name, MAX, stdin); /* it is the code */
}
Why is it happening and how do I fix it?
Don't mix scanf() with fgets() in the same code.
scanf("%d",&choice); does not consume all the line - it leaves the trailing '\n' for the later fgets() to consume as an empty line.
scanf() is difficult to use in a secure fashion.

Resources