C program to input an array from a sequence of space-separated integers Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article 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: Comment More infoAdvertise with us Next Article How to Find the Mode of Numbers in a Sorted Array in C? A ashokmahendrakar Follow Improve Article Tags : Strings Technical Scripter C Programs DSA Arrays c-input-output +2 More Practice Tags : ArraysStrings Similar Reads C program to sort an array using pointers Given an array of size n, the task is to sort this array using pointers in C. Examples: Input: n = 5, arr[] = {0, 23, 14, 12, 9} Output: {0, 9, 12, 14, 23} Input: n = 3, arr[] = {7, 0, 2} Output: {0, 2, 7} Approach: The array can be fetched with the help of pointers with the pointer variable pointin 2 min read C program to sort an array using pointers Given an array of size n, the task is to sort this array using pointers in C. Examples: Input: n = 5, arr[] = {0, 23, 14, 12, 9} Output: {0, 9, 12, 14, 23} Input: n = 3, arr[] = {7, 0, 2} Output: {0, 2, 7} Approach: The array can be fetched with the help of pointers with the pointer variable pointin 2 min read C program to sort an array using pointers Given an array of size n, the task is to sort this array using pointers in C. Examples: Input: n = 5, arr[] = {0, 23, 14, 12, 9} Output: {0, 9, 12, 14, 23} Input: n = 3, arr[] = {7, 0, 2} Output: {0, 2, 7} Approach: The array can be fetched with the help of pointers with the pointer variable pointin 2 min read How to Find the Range of Numbers in an Array in C? The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra 2 min read How to Find the Mode of Numbers in a Sorted Array in C? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will learn how to find the mode of all elements in a sorted array of integers in C. Example: Input:myArray = {1, 2, 3, 3, 5, 5, 5, 5, 6, 7} 2 min read C Program to Split a String into a Number of Sub-Strings In this article, we will learn how to split a string into a number of sub-strings using the C program.The most straightforward method to split a string into substrings using a delimiter is by using strtok() function. Letâs take a look at an example:C#include <stdio.h> #include <string.h> 3 min read Like