8.2 WORKING WITH STRINGS

Normal operations with strings are:

  • Reading a string
  • Writing a string
  • Comparing two strings
  • Copying one string into other.
  • processing strings

Let us see how these operations are handled in C++.

8.2.1 Reading a string

Like any other variable we can use following statement to read the string from the console.

cin >> str1;

Only point to remember is that blank is used as a terminator in this case. Well, we can use the function gets() or getline() for reading strings with embedded blanks.

8.2.2 Writing a string

A simple statement

cout << str1;

is capable of writing a string. Here embedded blanks are not a problem. If there are embedded characters like ‘\n’ or ‘\t’, they will be treated appropriately. For example

cout << ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.