System ProgrammingLearn About Strings in C++ Programming

Learn About Strings in C++ Programming


Strings is one of the important data types that every programming language should have support to. Your name, your car model, your home address, your job title, and your study major are all data of type string. This article will show you how to work with Strings in C++. Enjoy!!

C/C++ Classical View to Strings

“Strings are arrays of characters”, this is how the original C and C++ languages see strings. Based on this definition, the following is the syntax to declare a string:


STRINGNAME is the name of the characters array. It could be any valid C++ identifier.
STRLEN is the number of elements in the characters array. This is the maximum possible length for the string that will be stored in this array.

The following program prompts the user to enter his first name, and replies by printing a greeting.

#include <conio.h>
#include <iostream.h>
void main()
	char fname[12];
	cout << "Enter your First Name: ";
	cin >> fname;
	cout << "\nHave a nice day, " << fname << endl;

Let’s see what we get when executing this code:

Initializing Strings

Similar to an array (in its nature), a string can be initialized in the same way. The following statement defines and initializes a string variable greeting to the string “Good Morning”:

char greeting[] = "Good Morning";

Protecting Against Buffer Overflow

In the above example, we have defined the string (array) fname to be 12 characters (elements) long. What if the user has entered either intentionally or non-intentionally a string of more than 12 characters?! Actually, the program will not complain; and this is source of danger. This situation is called Buffer Overflow. In buffer overflow, a software program violates memory safety by exceeding the buffer (memory locations reserved for the array of characters in our case) and writing to adjacent memory locations. This could exploited to compromise data integrity (by writing to memory locations the program is not supposed to write to), or to inject malicious codes. Fortunately, C++ has a way to validate the length of the input string: the setw manipulator. The setw manipulator is used with the >> operator to set the limit on the number of characters that can be accepted by the input buffer. Now, the line that accepts the user’s first name could be re-written as follows:

cin >> setw(12) >> fname;

This will limit the maximum accepted characters to 12 as required.

For the program to compile successfully, and not complain about the setw manipulator, the following preprocessor directive should be written to include the header file iomanip.h

#include <iomanip.h>

Accepting Strings with White Spaces

Again, with the first name program: If you try to enter a name that consists of more than one word, you will be surprised that the program has accepted only the first one. Check this!

This is simply because the extraction operator >> uses the white space (the normal white space, tap, or new line) to mark the end of input. To overcome this problem and allow your program to accept multi-word strings, the cin.get() function is used instead.

The syntax for using cin.get():


The following program prompts the user to enter his full name, and prints it.

#include <conio.h>
#include <iostream.h>
void main()
	char fullname[30];
	cout << "Enter your Full Name: ";
	cout << "\nName:\t"  << fullname << endl;

When executed, the program should behave like this:

Learn the Basics of C Programming Language

Reading Multi-line Strings

Strings may also span multiple lines. Could C++ handle this situation? Fortunately, yes. The same function cin.get() can be overloaded to have another implementation that accepts besides to the string variable and the maximum acceptable string length, a third argument: a stop character. When this character is encountered by the cin.get() function, the function stops reading input.

The following program asks the user to enter his full name, his job, and then his address, each on a separate line. The program then prints the entered info.

#include <conio.h>
#include <iostream.h>
void main()
	char strdata[250];
	cout << "Enter your Full Name, Job Title, and Address "
	     << "(each on separate line). \nType # when finished:\n";
	cout << "\nYour Info:\n"  << strdata << endl;

Let’s see it in action:

When the cin.get() function encounters the ‘#’ character, it will stop reading.


In this article, we had an introduction to Strings.

  • Originally, C/C++ implemented strings as arrays of characters.
  • The extraction operator >> reads only single words.
  • To read multi-words strings, the get() function is used.
  • The get() has another implementation that accepts a termination character as a third argument that when encountered, the function stops reading input.

In the next article, we are going to continue with strings, and see how to copy, compare, and reverse strings. An interesting topic to wait for. See you.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -