C program to input an array from a sequence of space-separated integers
Given a string S consisting of space-separated integers, the task is to write a C program to take the integers as input from the string S and store them in an array arr[].
Examples:
Input: S = “1 2 3 4”
Output: {1, 2, 3, 4}Input: S = “32 12”
Output: {32, 12}
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Approach: The idea is to solve the given problem is to use getchar() function to check if a ‘\n’ (newline) occurs is found while taking input and then stop the input. Follow the step below to solve the given problem:
- Initialize a variable, say count, which is used to store the index of the array element.
- Initialize an array arr[] of size 106 to store the elements into the array.
- Iterate using a do-while loop until newLine occurs and perform the following steps:
- Store the current value at index count as scanf(“%d “, &arr[count]); and increment the value of count.
- If the next character is not endline, then continue. Otherwise, break out of the loop.
- After completing the above steps, print the elements stored in the array.
Below is the implementation of the above approach:
C
// C program for the above approach #include <stdio.h> // Driver Code int main() { // Stores the index where the // element is to be inserted int count = 0; // Initialize an array int a[1000000]; // Perform a do-while loop do { // Take input at position count // and increment count scanf("%d", &a[count++]); // If '\n' (newline) has occurred // or the whole array is filled, // then exit the loop // Otherwise, continue } while (getchar() != '\n' && count < 100); // Resize the array size to count a[count]; // Print the array elements for (int i = 0; i < count; i++) { printf("%d, ", a[i]); } return 0; } |
Output:
Article Tags :
Arrays
C Programs
Strings
Technical Scripter
c-input-output
Practice Tags :
Arrays
Strings
Read Full Article
“how to scan an array space separated integers in c” Code Answer
how to convert n space separated integers in c++
cpp by Filthy Finch on Jun 28 2020 Comment
0
Source: discuss.codechef.com
Add a Grepper Answer
C++ answers related to “how to scan an array space separated integers in c”
- input n space separated integers in c++
- how to store array of string with spaces in c++ stl
C++ queries related to “how to scan an array space separated integers in c”
- how to convert n space separated integers in c++
- how to read comma separated numbers into an integer array c++
- how to scan an array space separated integers in c
- c++ read space separated numbers
- how to print space separated integers in c++
- reading space separated integers from string c++
- how to print space-separated integers in c++
- how to input n space separated integers in c++
- how to return integers separated by spaces c++
- c++ convert string with spaces to numbers
- how to convert n space separated integers in c
- read input numbers separated by spaces in c++
- taking space separated input in c++
- how to take space separated int input in c++
- how to take n space separated input in c++
- how to read space separated string in c++
- how to interatr throught an input of numbers seperated by spaces in c
- how to read space separated integers in c
- input space separated integers in c++
- space separated input in c++
- space seperated ints to array c++
- how to take two space separated input in c++
- add numbers in c++ when numbers input with spaces
- reading space separated integers c++
- how to read space separated integers in c++
- n space separated integers in c++
- string of int separated by space c++
- take space separated input in c++
- c++ input integers separated by spaces
- read space separated integers from file in c++
- how to take input of n space input in c++
- c++ space separated integers
- how to take n space separated integer input in c++
- read space separated integers in c++
- how to read space seperated integers in cpp
- how to take space separated integer input in c++
- convert space separated string to int array c++
- store space separated integers t array in c++
- c++ convert number to space
- c++ print space separated integers
- how to read in numbers separated by space c++
- c++ string of numbers separated by space
- how to take space separated input in c++
- integers separated by a space c++
- how to input space separated integers in c++
- take user input seperated by spaces in c++
- how to take space separated different integer input in c++
- how to declare space-separated integers in cpp
A single line contains integers separated by space
885 Views
2 Comments
ShareSrinivasMaddeboina 10 mons ago
a=map(int,input().split()) for i in a: print(i,end=" ") its showing error in printing a single line containing l list of integers.Can anyone help me