×

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

C program to read string with spaces using scanf() function

Read string with spaces using scanf() function in C programming language - In this program we are going to explain how we can take input of a string with spaces?

Let's see what happened, when we read a string like another type of input

#include <stdio.h>
int main()
{
	char name[30];
	printf("Enter name: ");
	scanf("%s",name);
	
	printf("Name is: %s\n",name);
	return 0;
}

Output -1 (Enter name without space)

Enter name: Alex
Name is: Alex

Output -2 (Enter name with space)

Enter name: Alex Thomas
Name is: Alex

In both cases variable name stored only "Alex"; so, this is clear if we read a string by using "%s" format specifier, string will be terminated when white space found.

How to read string with spaces in C?

1) Read string with spaces by using "%[^\n]" format specifier

The format specifier "%[^\n]" tells to the compiler that read the characters until "\n" is not found.

Consider the program

#include <stdio.h>
int main()
{
	char name[30];
	printf("Enter name: ");
	scanf("%[^\n]",name);
	
	printf("Name is: %s\n",name);
	return 0;
}

Output

Enter name: Alex Thomas 
Name is: Alex Thomas

See the output, now program is able to read complete string with white space.


Problem -1 [Read string after integer input]

Here, we will read the person age then name and see what will happen? (i.e. we are reading the string after integer input)

Consider the program

#include <stdio.h>
int main()
{
    int age;
	char name[30];
	
	printf("Enter age: ");
	scanf("%d",&age);
	printf("Enter name: ");
	scanf("%[^\n]",name);
	
	printf("Name is: %s, age is: %d\n",name,age);
	return 0;
}

Output

Enter age: 23
Enter name: Name is: , age is: 23

Oh nooooooooooooooo!

The age input was successful but compiler doesn't wait to read name and moves to next statement which was printf("Name is: %s, age is: %d\n",name,age); and the output is "Enter name: Name is: , age is: 23" which we didn't expect.

Why this happened?

As we enter an integer value and hit enter to read next value, compiler stores either enter or null into the string's first character and string input terminates.

Here is the proof?


Here we are printing the value of string's first character by using printf("Name is: %d, age is: %d\n",name[0],age); the output will be "Enter name: Name is: 0, age is: 23"

Consider the program

#include <stdio.h>
int main()
{
    int age;
	char name[30];
	
	printf("Enter age: ");
	scanf("%d",&age);
	printf("Enter name: ");
	scanf("%[^\n]",name);
	
	printf("Name is: %d, age is: %d\n",name[0],age);
	return 0;
}

Output

Enter age: 23 
Enter name: Name is: 0, age is: 23

Here compiler stores, null (0) to the string's first character that is name[0]

How to fix it?

We have to read a character from input buffer and store it into temporary variable (remember - if we are going to read string or character after an integer or float (in some cases) input then we should read a temporary character which may available in the input buffer)

I am using a statement scanf("%c",&temp); before reading the string (which is going to be read after an integer input).

Consider the program

#include <stdio.h>
int main()
{
    int age;
	char name[30];
	char temp;
	
	printf("Enter age: ");
	scanf("%d",&age);
	printf("Enter name: ");
	scanf("%c",&temp); // temp statement to clear buffer
	scanf("%[^\n]",name);
	
	printf("Name is: %s, age is: %d\n",name,age);
	return 0;
}

Output

Enter age: 23 
Enter name: Alex Thomas 
Name is: Alex Thomas, age is: 23

See, now we are able to read complete string (with spaces) after taking an integer input.


Another way to read string with spaces in C

Using fgets()

fgets() function requires three parameters

  • char *s - character pointer (in which string will be stored)
  • int n - maximum number of character of the string
  • FILE *stream – a pointer of file stream, we can use “stdin”

fgets() will reads the complete string with spaces and also add a new line character after the string input.

Consider the program

#include <stdio.h>
int main()
{
    int age;
	char name[30];
	char temp;
	
	printf("Enter age: ");
	scanf("%d",&age);
	printf("Enter name: ");
	scanf("%c",&temp); // temp statement to clear buffer
	fgets(name,30,stdin);
	
	printf("Name is: %s, age is: %d\n",name,age);
	return 0;
}

Output

Enter age: 23 
Enter name: Alex Thomas 
Name is: Alex Thomas
, age is: 23



Comments and Discussions!

Load comments ↻





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