C program to input an array from a sequence of space-separated integers Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 6 Likes Like Report 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} 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: Create Quiz Comment A ashokmahendrakar Follow 6 Improve A ashokmahendrakar Follow 6 Improve Article Tags : Strings Technical Scripter C Programs DSA Arrays c-input-output +2 More Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 3 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like