Tricky Challenge using scanf! | CHALLENGE OF THE DAY 🔥😎 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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)

11th Dec 2017, 5:15 PM
Saurabh Verma
Saurabh Verma - avatar
20 Answers
0
Sorry about the misunderstood. Here it is https://code.sololearn.com/cTWCKtOuPis7/?ref=app
12th Dec 2017, 2:58 PM
Jonathan Álex
Jonathan Álex - avatar
+ 3
https://code.sololearn.com/c2I1OX9j0aQ4/?ref=app
11th Dec 2017, 7:20 PM
...
+ 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.
12th Dec 2017, 11:37 AM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
+ 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
12th Dec 2017, 4:39 PM
Saurabh Verma
Saurabh Verma - avatar
+ 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
12th Dec 2017, 9:09 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
+ 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.😊
13th Dec 2017, 3:23 PM
Saurabh Verma
Saurabh Verma - avatar
0
@Jonathan Alex, you are right about your concept, but here the challenge is to scan a sentence using scanf. so try..
12th Dec 2017, 2:36 AM
Saurabh Verma
Saurabh Verma - avatar
0
I misundestood the purpose of the challenge, sorry. I will work on this sollution right now.
12th Dec 2017, 12:28 PM
Jonathan Álex
Jonathan Álex - avatar
- 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]); }
11th Dec 2017, 6:57 PM
Harshita Jain
Harshita Jain - avatar
- 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
11th Dec 2017, 8:01 PM
Jonathan Álex
Jonathan Álex - avatar
- 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.
12th Dec 2017, 2:17 AM
Saurabh Verma
Saurabh Verma - avatar
- 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.
12th Dec 2017, 2:28 AM
Saurabh Verma
Saurabh Verma - avatar
- 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.😉
12th Dec 2017, 12:23 PM
Saurabh Verma
Saurabh Verma - avatar
- 1
@Saurabh Verma thanks my friend! Don't forget to like my code and leave a comment if you want
12th Dec 2017, 4:41 PM
Jonathan Álex
Jonathan Álex - avatar
12th Dec 2017, 8:58 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
- 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.😊
13th Dec 2017, 2:05 AM
Saurabh Verma
Saurabh Verma - avatar
- 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.
13th Dec 2017, 10:37 AM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
- 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
13th Dec 2017, 2:16 PM
Saurabh Verma
Saurabh Verma - avatar
- 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.
13th Dec 2017, 2:32 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
- 2
@Saurabh Verma I agree.
13th Dec 2017, 3:42 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar