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.

--

--

Swati Tripathi

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