×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

How can I read an input string of unknown length in C language?

In this tutorial, we will learn how we can read an input string of unknown length in C programming language?
Submitted by Shubh Pachori, on July 03, 2022

Strings in the C programming language are very integral parts. So, when a beginner starts coding he may have a good understanding of the concepts of array and strings. Both Arrays and Strings are widely used topics. As we know that if we have to read an array or string of known length we have to use loops for it. Both Arrays and Strings can be read by the use of looping only if their length is known to the coder.

Reading a known length of array and string and to print them in C

#include <stdio.h>

int main()
{
    int i; //declaring a variable for looping
    int arr[5]; //declaring a array of length 5
    char str[5]; // declaring a string of length 5

    for (i = 0; i <= 4; i++) //for loop for reading input of array
    {
        printf("Enter in Array at %d:", i);
        fflush(stdin); //it is a command to clear the keyboard memory
        scanf("%d", &arr[i]);
    }

    for (i = 0; i <= 4; i++) //for loop for reading input of string
    {
        printf("Enter in String at %d:", i);
        fflush(stdin); //it is a command to clear the keyboard memory
        scanf("%c", &str[i]);
    }

    printf("Array:");
    for (i = 0; i <= 4; i++) //for loop for printing array
    {
        printf("%d", arr[i]);
    }

    printf("\n");

    printf("String:");
    for (i = 0; i <= 4; i++) //for loop for printing string
    {
        printf("%c", str[i]);
    }

    return 0;
}

Output:

Example 1: Read an input string of unknown length

In the above code, we can see that the above method can only be used if we have the length of the string. But it is not applicable for the string whose length is unknown to us. For this problem, there is a format specifier %s by which we can read a string of unknown length and we don't need looping to read and print the string in the code. It results in less time taken for compiling the code by the compiler.

Reading an unknown length of string and printing them using %s format specifier in C

#include <stdio.h>

int main()
{
    int roll_no;
    // declaring a string
    char name[30];

    printf("Enter Roll Number:");
    scanf("%d", &roll_no);

    printf("Enter Name:");
    //%s format specifier is used to read string
    scanf("%s", &name);

    // %s format specifier is used to print string
    printf("Roll Number:%d\nName:%s", roll_no, name);

    return 0;
}

Output:

Example 2: Read an input string of unknown length

As we can see in the above code the %s format specifier doesn't read the string after the space. So, %s format specifier is not as useful for reading an unknown length of the string. So, here is a gets() function to read the whole string that is entered by the user. It is mostly used function for reading an unknown length string.

For Example: As we write a code for a school that wants to maintain its student's records like its roll number, name, etc. So, the name of every student is not of the same length and same spacing so looping and %s format specifier is not ideal for the reading of students' names whose length is unknown to us. So, in that case, the gets() function is used to read an unknown length string.

Reading an unknown length of string and print them using gets() function in C

#include <stdio.h>

int main()
{
    int i, roll_no;
    char name[30];
    char about[50];

    printf("Enter Roll Number:");
    // command to clear the keyboard memory
    fflush(stdin);
    scanf("%d", &roll_no);

    printf("Enter Name:");
    // command to clear the keyboard memory
    fflush(stdin);
    // gets() function to read string
    gets(name);

    printf("Write a Few Lines About Yourself:");
    fflush(stdin);
    gets(about);

    // %s format specifier is used to print string
    printf("Roll Number:%d\nName:%s\n", roll_no, name);

    printf("About:%s", about);

    return 0;
}

Output:

Example 3: Read an input string of unknown length



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.