Strings as Input
Learn how to take strings (letters, words, and sentences) as input.
Taking strings as input from the console
There are three standard ways to take a string as input from the console.
1. In the form of letters
In the first one, we take an ordinary integer array. We make a loop and keep taking the input character by character until the stream reader (cin
) reads the delimiter character (which the user can define by themselves), for example, in the below code, we considered '.'
as delimiter).
The
cin>>achar
does not read any of the following characters:
' '
\t
\n
\r
char s[11]; int size=0;while(size<10){char ch;cin>>ch; // say user entered "h e l l o." this ignores all the space charactersif(ch!='.') // dot is the delimiter characters[size]=ch; size++;}s[size] = '\0';cout << s << endl; /* surprisingly cout will keep writing starting from s[] base addresscharacter by character until it reaches '\0' character. This means writing '\0'in the end is important, if you miss it you will see some garbage letters in the end. */
2. In the form of words
Surprisingly, in contrast to integer arrays, if we use cin >> astring
, the compiler allows it and takes the input from the console character by character until it encounters any delimiter character ({' ', '\t', '\n', '\r'}
).
char s1[100], s2[100], s3[100];cin>>s1; // Write 3 words "The Albert Einstein" and press ENTER// s1 will only receive "The"cin>>s2>>s3; // s2[] will receive "Albert" and s3[]="Einstein"cout<<"s1[]="<<s1<<endl;cout<<"s2[]="<<s2<<endl;cout<<"s2[]="<<s2<<endl;
3. In the form of sentences
The cin.getline()
is a built-in function that allows us to read single or multiple string lines from the input stream.
cin.getline(char Array[], int NoOfCharactersAtMax, char Delimiter_Character);
This method is helpful if we want to read a stream of characters until there is a delimiter character in the stream. The default delimiter character is '\n'
, which can be replaced with any other character.
Look at the syntax of this function below.
Let’s write a code, execute it step by step, and see the output:
// String Declarationchar nickname[200];cin.getline(nickname,200,'\n');// "nickname" is the string name// Assume that 200 is the maximum string array that can be loaded from the console,// '\n' is the default delimitercin.getline(nickname,200,'.');// dot (.) as delimiter to stop the input from the input stream
There is geometry in the humming of the strings, there is music in the spacing of the spheres. Pythagoras#
Try it yourself
All the above methods of taking an input from the console can be replicated to the file stream as well, one example is given in the above playground. You should try all of the above ways by taking input from the file in one of the following ways:
- Letter by letter
- Word by word
- Line by line
- Sentence by sentence or paragraph by paragraph
Instruction: Change the above code by taking all the input strings from the file.
Exercise: Reading the quote from a file
Look at quote.txt
. It has the following text:
There is geometry in the humming of the strings, there is music in the spacing of the spheres.
Pythagoras
Read the entire line till dot '.'
. Followed by a word (having the name of the author).