Ways to take character array as an input in C++

Swati Tripathi
Sep 14, 2021

--

Take string or character array as input in C++.

Following are few ways to do this:

  1. gets(arr)
  2. scanf(“%s”, arr)
  3. scanf(“%[^\n]”, &arr)
  4. fgets(arr, 20, stdin)

Following is the program to show you some ways to take character array/ string as input in C++.

You can also observe in this program which method of taking string as input allows whitespaces and which does not. For example, scanf(“%s”, arr) does not take input after white space is encountered whereas gets(arr) does not mind the whitespaces.

C++ Program

Output of Program

Any suggestions to improve the article is welcome! You can connect with me on Twitter.

Check out my GitHub account by clicking here.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Swati Tripathi
Swati Tripathi

Written by Swati Tripathi

I believe everyone should hustle hard to make a positive impact!

No responses yet

Write a response