c - can't get multiple strings with scanf() - c

I've been struggling to get two lines seperated with space and my scanf() is not working properly
This is the code
char *str1, *str2;
while(1) {
printf("Enter string:\n");
scanf(" %s", str1);
printf("Then;");
scanf(" %s", str2);
if(strcmp(str1, "exit") == 0){break;}
printf("Output:\n %s %s\n", str1, str2);
}
But my output seems like this:
Enter string:
ok
Then;hello
Output:
ok (null)
Enter string:
Then;
What would cause this problem? And it prints first output as the loop turns second time.
Enter string:
ok
Then;hello
Output:
ok (null)
Enter string:
Then;well
Output:
hello (null)
Enter string:
Then;no
Output:
well (null)
Enter string:

"%s" in scanf(" %s", str1); expects str1 to point to valid memory to store a string. str1 in not initialized - it does not point to valid memory.
Instead provide valid memory and limit input width.
char str[100];
scanf("%99s", str1);
The array str is converted to a pointer to the beginning of the array when passed to scanf("%99s", str1);
The leading space in " %s" is not needed as "%s" itself consumes leading white-space,
just like " ".

Related

C program strings

I run the following c program
char str[80] = "String";
printf("hello %s\n", str);
scanf("%s", str);
printf("%s\n", str);
scanf("%[ABCDEF]", str);
printf("hello %s\n", str);
return 0;
For some reason on line 5 when it is suppose to input from Pattern %[ABCDEF], the console simply prints previous string (input from line 3). Why is that so?
Thats because the first scanf call doesn't read the newline character and the second call to scanf simply reads that newline character. To avoid this start the format string with a space like this:
#include <stdio.h>
int main(void)
{
char str[80] = "String";
printf("hello %s\n", str);
scanf("%s", str);
printf("%s\n", str);
scanf(" %[ABCDEF]", str);
printf("hello %s\n", str);
return 0;
}
However, you also need to make sure that str doesn't overflow if the user inputs a string longer than 79 characters.

Is there possible to write in a string words and use ate the same time %s as we do in printf()?

I am wondering is it possible to write in a string some content and put another string in there at the same time
char str[50], *test;
scanf("%s", str);
test = "123 %s 123", str;
printf("%s\n", test);
consider i write in the scanf "abc"
Is the a way for it to output:
123 abc 123
You can use snprintf() to do printf-like formatting and put the result into character array.
char str[50], test[128];
scanf("%s", str);
snprintf(test, sizeof(test), "123 %s 123", str);
printf("%s\n", test);

printf is not working as expected. Can someone explain the output?

I am trying to read two strings from the keyboard and print them.
Why does printf("read 1st\n") run after the second scanf()?
#include <stdio.h>
int main(void)
{
char str[20];
char str2[20];
scanf("%s", str);
printf("read 1st\n");
scanf("%s", str2);
printf("read 2nd\n");
printf("str: %s\nstr2: %s\n", str, str2);
return 0;
}
Expected:
foo
read 1st
bar
read 2nd
str: foo
str2: bar
Actual:
foo
bar
read 1st
read 2nd
str:foo
str2:bar
I could not reproduce your problem, but adding a fflush(stdout); should take care of your problem.
scanf("%s", str);
printf("read 1st\n");
fflush(stdout); // Ensures that the above is printed before scanf is executed
scanf("%s", str2);
printf("read 2nd\n");
I would also change the scans to scanf("%19s", str);. Otherwise, bad things could happen if you enter a string longer than str can hold.

When I try to input a string that contains spaces it's supposed to go to the error message, it does not do that?

when I try to input a string that contains a semi colon it goes to the error message( please enter a string without any spaces) similarly if I try to enter a string with spaces it still goes to the error message.
what my program should do is if the input does not contain a space run the program else go to the error message.If the input contains a semi colon but no spaces run the program.
printf("Enter a string\n");
scanf("%20s", string_buffer);
stringp1 = string_buffer;
The format specifier %s doesn't read blank spaces at all. Let's take a look at this code snippet:
scanf("%s", string);
puts(string);
Input :
Hello, my name is Claudio
The output will be :
Hello,
To avoid that you should use an inverted group scansion inside of your scanf instruction, just like this one :
scanf("%string_size[^\n]", string);
This will do the trick.
By the way, I would advise you to use other function like getline or fgets to take string from stdin.
Note that scanf will break at spaces. So, if you have this code:
scanf("%20s", string_buffer);
And you enter "hello there" is the input, then the contents of string_buffer will be only "hello".
try this
#include <stdio.h>
#include <string.h>
int main(void){
char string_buffer[20];
char *stringp;
printf("Enter a string\n");
scanf("%19[^\n]", string_buffer);//20 --> 19, include space
stringp = string_buffer;
if (strstr(stringp, " ") == NULL) {//!= --> ==
printf("Converted semicolon to 4 spaces\n");
for (; *stringp; stringp++) {
if (*stringp == ';'){//strsep is not needed
printf(" ");
} else {
printf("%c", *stringp);
}
}
} else {
printf("Please enter a string without any spaces\n");
}
printf("\n");
return 0;
}
scanf("%20s", string_buffer);
Use fgets instead of scanf as %s in scanf matches a sequence of non-white-space characters only.
So if you enter foo bar, it will only accept foo and not the whole string.
if (strstr(&stringp1, " ") != 0)
strstr expects both arguments to be char * and here first argument is char**.
Remove & address of operator.
if (strstr(&stringp1, " ") != 0)
If substring is found then strstr returns char * pointer to substring else it returns NULL.
That means this if expression will be true if there's a white space in the string. And we want an error message to be printed for that. So clearly reverse condition.
Solution: change that if to
if (!strstr(stringp1, " "))
So the correct code snippet would be:
...
printf("Enter a string\n");
fgets(string_buffer, 20, stdin);
stringp1 = string_buffer;
if (!strstr(stringp1, " ")) {
...

How do I properly 'printf' an integer and a string in C?

I have the following code:
char *s1, *s2;
char str[10];
printf("Type a string: ");
scanf("%s", str);
s1 = &str[0];
s2 = &str[2];
printf("%s\n", s1);
printf("%s\n", s2);
When I run the code, and enter the input "A 1" as follow:
Type a string: A 1
I got the following result:
A
�<�
I'm trying to read the first character as a string and the third character as an integer, and then print those out on the screen. The first character always works, but the screen would just display random stuffs after that.... How should I fix it?
You're on the right track. Here's a corrected version:
char str[10];
int n;
printf("type a string: ");
scanf("%s %d", str, &n);
printf("%s\n", str);
printf("%d\n", n);
Let's talk through the changes:
allocate an int (n) to store your number in
tell scanf to read in first a string and then a number (%d means number, as you already knew from your printf
That's pretty much all there is to it. Your code is a little bit dangerous, still, because any user input that's longer than 9 characters will overflow str and start trampling your stack.
scanf("%s",str) scans only until it finds a whitespace character. With the input "A 1", it will scan only the first character, hence s2 points at the garbage that happened to be in str, since that array wasn't initialised.
Try this code my friend...
#include<stdio.h>
int main(){
char *s1, *s2;
char str[10];
printf("type a string: ");
scanf("%s", str);
s1 = &str[0];
s2 = &str[2];
printf("%c\n", *s1); //use %c instead of %s and *s1 which is the content of position 1
printf("%c\n", *s2); //use %c instead of %s and *s3 which is the content of position 1
return 0;
}

Resources