Search⌘ K
AI Features

Inside the String

Explore key concepts of C++ strings including how to initialize, access individual characters, and concatenate strings. Understand practical applications through programming exercises like reversing strings, shifting characters, and checking palindromes. This lesson builds foundational skills for handling text data in C++ programming.

Collection

A collection is a general term used to group multiple values into a single unit. We use collections all the time in the real world―a book is a collection of pages, a train is a collection of train cars, and a wardrobe is a collection of clothes.

String

A string is a collection of characters, mostly treated as a single unit. A string is used to store text values like a name, address, message, etc.

Initializing the string

The following program demonstrates the string initialization:

C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string string1 = "The is a string.";
cout << string1 << endl;
return 0;
}

Note: We have to write #include <string> at line 2 to include a header file string that allows the use of string data type in the program.

In the above code:

  • At line 7, we initialize string1 with a text value enclosed in double quotes " ".
  • At line 8, we display it.

Accessing the characters in a string

C++ allows us to access individual characters inside a string through an integer index number. To do this, we enclose the index number in square brackets [ ] after the string variable name, as shown in the following program:

C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string string1 = "Educative";
cout << string1 << endl;
cout << string1[0] << endl; // Displaying first element of string
cout << string1[1] << endl; // Displaying second element of string
cout << string1[2] << endl; // Displaying third element of string
cout << string1[3] << endl; // Displaying fourth element of string
cout << string1[4] << endl; // Displaying fifth element of string
cout << string1[5] << endl; // Displaying sixth element of string
cout << string1[6] << endl; // Displaying seventh element of string
cout << string1[7] << endl; // Displaying eighth element of string
cout << string1[8] << endl; // Displaying ninth element of string
return 0;
}

The code above displays the individual characters of a string on separate lines, one by one. The first character is at 0 index, and the index moves forward along the string in linear increments, as illustrated in the following figure:

The same program can be rewritten using loops as demonstrated below to perform the same task.

C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string string1 = "Educative";
cout << string1 << endl; // Displaying the whole string
int length = 9; // Storing the length of the array
for (int i = 0; i < length; i++) // for loop to display the string characters individually
{
cout << string1[i] << endl;
}
return 0;
}

Concatenation

The joining of two strings with the help of the + operator is called concatenation, illustrated in the following code.

C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "Hello";
string str2 = "Educative";
string str3 = str1 + str2;
string str4 = str1 + " " + str2;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl; // Concatenated without space
cout << str4 << endl; // Concatenated with space
return 0;
}

In the code above:

  • Line 9: We concatenate str1 and str2 and store the result in str3.It joins the two words without a space.
  • Line 10: We concatenate str1, " ", and str2 and store the result in str4. It joins the two words with a space.

Practice for string manipulation

The following are a few example programs to practice using strings in C++. Clicking the “Show Solution” button will display a program that solves the respective problem. You may copy and paste the given solution into the code widget to make sure the output of your solution matches the given solution. There may be several ways of writing correct solutions in programming.

Print the characters backward

Write a program that displays the characters of a string backward, line by line. The length of the string is restricted to nine characters.

Sample input

"Educative"

Sample output

e
v
i
t
a
c
u
d
E
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Educative";
// Write your code here.
return 0;
}

Left shift the characters in a string

Write a program that shifts the characters of a string to the left and replaces the last position with a dot. The length of the string is restricted to nine characters.

Sample input

"Educative"

Sample output

ducative.
C++
#include <iostream>
#include <string>
int main()
{
string myString = "Educative";
// Write your code here
return 0;
}

Reverse the string

Write a program that stores a string into another string in reverse order. The length of the string is restricted to nine characters.

Sample input

"Educative"

Sample output

evitacudE
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myString = "Educative";
// Write your code here.
return 0;
}

Palindrome test

A palindrome is a sequence of characters that reads the same backward and forward. Write a program that checks if a string is a palindrome. Consider the string with the length of five characters.

Sample input 1

"level"

Sample output 1

Palindrome

Sample input 2

"legal"

Sample output 2

Not palindrome
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "level";
// Write your code here.
return 0;
}