Ways to take character array as an input in C++
Sep 14, 2021
Take string or character array as input in C++.
Following are few ways to do this:
- gets(arr)
- scanf(“%s”, arr)
- scanf(“%[^\n]”, &arr)
- 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.