+ 4
Tricky Challenge using scanf! | CHALLENGE OF THE DAY 🔥😎
Code a C program to read a sentence (containing space) from user input and store it into a array of character, and then display it using *printf*. Use only *scanf* function to read the user input. Code should be least. /* char x[64]; //here your *scanf* code //here your *printf* code */ Good Luck! EDIT: Use the following input to check if your code works. input : Is my "code" working? output must be exact same as above (with all space and double quote symbol and question mark)
20 Answers
0
Sorry about the misunderstood. Here it is
https://code.sololearn.com/cTWCKtOuPis7/?ref=app
+ 3
https://code.sololearn.com/c2I1OX9j0aQ4/?ref=app
+ 1
Multiple reading of array x with printing:
https://code.sololearn.com/c161Rf777z9a/?ref=app
This code will store all the characters you enter, including chains of several spaces and other white space characters. In addition, I expanded its capabilities by re-reading and outputting the array until the input stream is exhausted.
+ 1
Perfect @Jonathan Alex, you are the winner.😆😉😊
Here is the same code which I have written, now I have made it public.😋
https://code.sololearn.com/cqaaV2ZBGb31/?ref=app
+ 1
@Saurabh Verma
My code, unlike yours, reads and displays also multiline texts that include '\n'.
I also set up your code. Now he too can become several lines.
https://code.sololearn.com/cKWcGoMjSpxg/?ref=app
+ 1
@Vadim Sukhotin
I got what you are trying to say, but the Challenge is to scan *a sentence*, and a sentence contain only one line. Hope you are clear with my challenge.
Your both code is right, and works properly. But here challenge is to scan only a sentence.😊
0
@Jonathan Alex, you are right about your concept, but here the challenge is to scan a sentence using scanf. so try..
0
I misundestood the purpose of the challenge, sorry. I will work on this sollution right now.
- 1
According to me following diagram will help you:
/* inside main program */
char x[64];
int i;
printf("Enter a sentence (containing space)");
for(i=0;i<x.lentgth;i++){
scanf("%c",&x[i]);
}
- 1
It's ok using scanf. A for loop and the adress simbol (&) should solve. The only thing is that it will keep storing the string until a space appears.
If you enter "Hello world", it will store only "Hello".
That's why we use gets( ) instead of scanf( ) for strings
- 1
@Swapnil More,
Your code will only scan single word. it will not scan the whole sentence.
and the challenge is to scan the whole sentence using scanf.
See the EDIT section in my question.
- 1
@Harshita Jain
I haven't tested your code yet. But I think your code is WRONG.
What will happen if my sentence length is only 30 suppose, your program will ask to enter more characters until it reaches to length 64.
The challenge it to scan any sentence of any length less than 64.
- 1
@Vadim Sukhotin, your code is working properly.😊 Nice try, and +1 for that.
But it can also be done without using any loops.
So, the code will be least and then the challenge will be completed.😉
- 1
@Saurabh Verma thanks my friend! Don't forget to like my code and leave a comment if you want
- 1
@Vadim Sukhotin, the scanf function can be manipulated in various ways, in which the "\0" is also included.
btw, that's for the effort, and +1 for the code.😊
- 1
@Saurabh Verma
I can not imagine a case where you need to enter a character with the code 0 - '\0' from the keyboard. If by some miracle this symbol is entered (for example, by Alt-input), it will be perceived as a symbol of the end of the line and will not be displayed anyway. Therefore, the restriction of [^\0] allows you to enter all the real characters at all.
- 1
@Vadim Sukhotin,
if you go through following program, it will accepts almost all characters you can think of (in my opinion) (eg: \n, \0, \t or anything).
If the input is a single sentence within given length (no matter what is the input), this code will work perfectly.
and if there is anything exception, then let me know.
Thanks for your effort.
https://code.sololearn.com/cqaaV2ZBGb31/?ref=app
- 1
@Saurabh Verma
If I input two line or more, your code prints only first line of my input. I input
1234
1324
and program print
1234
And my version of your code print all lines of input (https://code.sololearn.com/cKWcGoMjSpxg/?ref=app). Also my own code does this (https://code.sololearn.com/cq9PjDQr52SZ/?ref=app).
Еntering the symbol \ 0 does not make sense, since it is a sign of the end of the line. When you print a line in which it is inserted, printing on it will end.
- 2
@Saurabh Verma
I agree.