Prerequisite: getline(string) in C++
In C++, stream classes support line-oriented functions, getline() and write() to perform input and output functions respectively. getline() function reads whole line of text that ends with new line or until the maximum limit is reached. getline() is the member function of istream class and has the syntax:
// (buffer, stream_size, delimiter) istream& getline(char*, int size, char='\n') // The delimiter character is considered as '\n' istream& getline(char*, int size)
The function does the following operations:
1. Extracts character up to the delimiter.
2. Stores the characters in the buffer.
3. Maximum number of characters extracted is size – 1.
Note that the terminator(or delimiter) character can be any character (like ‘ ‘, ‘, ‘ or any special character, etc.). The terminator character is read but not saved into a buffer, instead it is replaced by the null character.
Your Name is:: Aditya Rakhecha
In the above program, the statement cin.getline(str, 20) reads a string until it encounters the new line character or maximum number of characters (here 20). Try the function with different limits and see the output.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- getline (string) in C++
- std::basic_istream::getline in C++ with Examples
- How to use getline() in C++ when there are blank lines in input?
- Convert character array to string in C++
- Check input character is alphabet, digit or special character
- array get() function in C++ STL
- array at() function in C++ STL
- How to return a local array from a C/C++ function?
- Declare a C/C++ function returning pointer to array of integer pointers
- Character arithmetic in C and C++
- Character Classification in C++ : cctype
- How to convert a single character to string in C++?
- Change/add only one character and print '*' exactly 20 times
- Data type of character constants in C and C++
- Type difference of character literals in C and C++
- Frequency of each character in a String using unordered_map in C++
- Program to count occurrence of a given character in a string
- Character replacement after removing duplicates from a string
- Print a character n times without using loop, recursion or goto in C++
- array::front() and array::back() in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.