Explains the number of methods from which we can take the string input from the user.
There are some methods that are used to take the string input from user. Let's discuss it.
Method1.
you can create a character array for multiple strings.
for example:- char a[size];
Now, you can input a string in the form of array. In the character array, when you enter the string that will be a sequence of characters and each character will be element of the array. In which the string will end with a special character that is \0. So, you can put it at the end of the character.
Method2.
you can use cin.get() method that accepts the character with whitespace. So, it reads the string with whitespace. you can enter the string character by character using loop.
for example:- cin.get(string name, size);
Method3.
you can use gets() method to take user input that reads the character from stdin and stores them in str until a newline character is found.
for example:- gets(str);
Method4.
you can use cin.getline() method that is used to read unformatted string means set of characters from the keyboard(standard input device).
for example:- cin.getline(<character array>, size);
Method5.
you can use simple cin method with extraction operator >> to take a string input from the user. But it doesn't allows the whitespace. Whitespace is a delimiter for cin.
for example:- cin>>enter name;
There are another method in c++ provides us with the header file <cstring>. By this file, you can find lot of predefined functions for string. You can create variables of string data type by the header file.
Step by step
Solved in 2 steps