Inside the String
Explore how to work with strings in C++, including initializing strings, accessing characters by index, concatenating multiple strings, and creating programs to manipulate strings such as reversing or left shifting characters. This lesson helps you practice essential string operations useful for problem-solving.
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:
Note: We have to write
#include <string>at line 2 to include a header filestringthat allows the use ofstringdata type in the program.
In the above code:
- At line 7, we initialize
string1with 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:
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.
Concatenation
The joining of two strings with the help of the + operator is called concatenation, illustrated in the following code.
In the code above:
- Line 9: We concatenate
str1andstr2and store the result instr3.It joins the two words without a space. - Line 10: We concatenate
str1," ", andstr2and store the result instr4. 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
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.
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
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