+ 2

How to take multiple inputs in the same line? Number of inputs is not given.

include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int k,n; scanf(" %d %d", &n,&k); char tt[n+1]; scanf(" %s", &tt); int ids[k]; for(int l=0;l<k;l++) { scanf(" %d", &ids[l]); } int u=0; if((k>=1)&&(k<=n)&&(n<=1000)) {for(int i=0;i<n;i++) { if((int)tt[i]==0) { for(int y=0;y<k;y++) { if(i==ids[y]); u++; } } }} if(u==0) { printf("NO"); } else { printf("YES"); } return 0; }

18th Aug 2019, 5:49 AM
Gayani Abeykoon
Gayani Abeykoon - avatar
11 Answers
+ 2
Are there any possible ways to get k inputs in the same line without going to new lines?
18th Aug 2019, 7:12 AM
Gayani Abeykoon
Gayani Abeykoon - avatar
+ 1
since you're using a for loop, it wi be best if you give your inputs on a separate lines
18th Aug 2019, 7:16 AM
āœ³AsterisKāœ³
āœ³AsterisKāœ³ - avatar
+ 1
yep
18th Aug 2019, 7:31 AM
āœ³AsterisKāœ³
āœ³AsterisKāœ³ - avatar
+ 1
What about saving the string on a separate file and then use a loop with fscanf until EOF?
18th Aug 2019, 10:27 AM
Lea
+ 1
I mean putting the input of the user in a string with fgets, copying it onto a file and then reading the file with fscanf until EOF, putting every integer in an array
18th Aug 2019, 10:37 AM
Lea
+ 1
You may take the line as a String input and use StringTokenizer split it using the delimiter(s) of your choice. Scanner i = new Scanner(System.in); String total = s.nextLine(); StringTokenizer stok = new StringTokenizer(total, " "); //The double quotes contain all the delimiters. int n = stok.countTokens(); String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = stok.nextToken();
23rd Dec 2019, 4:27 AM
Param Siddharth
Param Siddharth - avatar
0
the user have to input the first scanf and a space for the second input, the second scanf on a newline and the third on a several newlines depending on the k input, i will say this will be complicating to use on SoloLearn, it will better of if you chip in all necessary inputs and allow the code just to run
18th Aug 2019, 7:08 AM
āœ³AsterisKāœ³
āœ³AsterisKāœ³ - avatar
0
If I am not using any loop I think there is no way to take k number of inputs in the same line while k depends on user's input.
18th Aug 2019, 7:30 AM
Gayani Abeykoon
Gayani Abeykoon - avatar
0
Can't we do it in java?
18th Aug 2019, 7:32 AM
Gayani Abeykoon
Gayani Abeykoon - avatar
0
Lea it should be user input.
18th Aug 2019, 10:34 AM
Gayani Abeykoon
Gayani Abeykoon - avatar
0
Lea thank you.
18th Aug 2019, 10:48 AM
Gayani Abeykoon
Gayani Abeykoon - avatar