January 13, 2021

C++ String Manipulation

In this lesson, you will learn about string manipulation in C++. We’ll cover all the various operations that you can do with strings.

  1. C-Type Strings
  2. C++ String Object
  3. String Manipulation Functions from C
  4. String Manipulation with C++ String Class


1. C-Type Strings

As you know, C++ supersets the C language. Therefore, it supports string functionalities from C. This is also called C-String. Strings in C are 1-dimensional array of characters that is terminated by a null character, ‘\0’ (ASCII code for 0).

To define a C-string in C++ use the code below;

char name[] = "Kindson";

In the code above, the string name is created and holds 7 characters.

char str[4] = "Kindson";
char str[] = {'K','i','n', 'd', 's', 'o', 'n', '\0'};

char str[4] = {'H','e','l', 'l', 'o', '\0'};

The program below uses a C-type string to read a name from the input. Then displays the name to the output.

int main () {
	//you must provide a size
	char name[20];
	cout << "Please enter your name: ";
	cin >> name;
	cout << "Hello " << name << endl;


2. C++ String Object

C++ also provides it’s own string class. So you can create string object. This class is provided by the standard C++ library. Unlike character array from C, the C++ string does not have a fixed length.  For example, the code below, reads and prints the user’s name but without knowing the size of the name:

// Example with C++ String
int main () {
	// size is not needed
	string name;
	cout << "What is your name: ";
	cin >> name;
	cout << "Welcome " << name << endl;


3. String Manipulation Methods (from C)

These are methods used for string manipulation. You need to understand these functions to be get things done with strings.

See table below:

SNMethod and description
1strcpy(string1, string2)

Copies string string2 into string string1.

2strcat(string1, string2)

Concatenates string string2 onto the end of string string1.


Returns the length of string string1.

4strcmp(string1, string2)

Returns 0 if string1 and string2 are the same; less than 0 if string1<string2; greater than 0 if string1 > string2.

5strchr(string1, ch);

Returns a pointer to the first occurrence of character ch in string string1.

6strstr(s1, s2)

Returns a pointer to the first occurrence of string string2 in string string1.

The functions above derives from C. Therefore to use them, you need to include <cstring> header in your program.


4. String Manipulation With C++ String Class

Let’s now see how to do the same thing using C++ string class

  • Concatenation – Combines two string into one. Simply use the + operator. See example below
  • String length – Use length() method
  • Searching strings – Accessing a character within a string. Use the find() method
  • Substrings – Returning part of a string. Use the substr() method.
  • Replacing – Replacing part of a string. Use the replace() method
  • Insertion – Inserting character(s) into a string. Using the insert() method
  • Erase – Removing part of a string. Use the erase() method.

The program below shows how these methods can be applied.

// String manipulation using methods
int main () {
	   string string1 = "Beginner ";
	   string string2 = "to Expert ";
	   string string3 = "Tutorials";
	   string string4 = string1 + string2 + string3;
	   int  len = string4.length();

	   cout << string4 << endl;

	   cout << "Length of string1 is: " << len <<endl;

	   cout <<"Expert is at position " << string2.find("Expert") <<endl;

	   cout << "Part of string 2: " << string2.substr(3,8)<<endl;

	   cout << "Replacing 'Expert':  " << string4.replace(12, 17, "Guru")<<endl;

	   cout << "Insertion:  "<< string4.insert(0, " by Kindson")<<endl;

	   cout << "Erasing:  " << string3.erase(0,3)<<endl;

	   return 0;

If you successfully run the program, you will have the output:

Beginner to Expert Tutorials
Length of string1 is: 28
Expert is at position3
Part of string 2: Expert 
Replacing 'Expert':  Beginner to Guru
Insertion:   by KindsonBeginner to Guru
Erasing:  orials

I would recommend you take some time to understand how it works to produce the outputs.

Leave a Reply

Your email address will not be published. Required fields are marked *