String Manipulation in C++

In C++, a string is a sequence of characters represented as an array of characters. Strings are used extensively in programming for tasks like storing text, processing input/output, and manipulating data. This chapter will cover various string manipulation techniques in C++, starting from the basics and progressing to more advanced topics.

String Operations

Strings in C++ are handled using the standard library std::string class. Here are some fundamental operations:

String Declaration and Initialization

In C++, you can declare and initialize strings using the std::string class.

				
					#include <iostream>
#include <string>

int main() {
    // Declaration and initialization
    std::string str = "Hello, World!";
    std::cout << str << std::endl;
    return 0;
}

				
			
				
					// output //
Hello, World!

				
			

Explanation:

  • We include the necessary header files <iostream> and <string> for input/output operations and string handling respectively.
  • We declare a string variable str and initialize it with the value "Hello, World!".
  • Finally, we print the string using std::cout.

String Concatenation

You can concatenate two strings using the + operator.

				
					#include <iostream>
#include <string>

int main() {
    std::string str1 = "Hello, ";
    std::string str2 = "World!";
    std::string result = str1 + str2;
    std::cout << result << std::endl;
    return 0;
}

				
			
				
					// output //
Hello, World!

				
			

Explanation:

  • We declare two string variables str1 and str2 and initialize them with "Hello, " and "World!" respectively.
  • We concatenate the two strings using the + operator and store the result in another string variable result.
  • Finally, we print the concatenated string.

String Length

You can get the length of a string using the length() function.

				
					#include <iostream>
#include <string>

int main() {
    std::string str = "Hello";
    std::cout << "Length of string: " << str.length() << std::endl;
    return 0;
}

				
			
				
					// output //
Length of string: 5

				
			

Explanation:

  • We declare a string variable str and initialize it with the value "Hello".
  • We use the length() function to get the length of the string and print it using std::cout.

String Input and Output

Reading String from User Input

You can read a string from the user using std::getline().

				
					#include <iostream>
#include <string>

int main() {
    std::string name;
    std::cout << "Enter your name: ";
    std::getline(std::cin, name);
    std::cout << "Hello, " << name << "!" << std::endl;
    return 0;
}

				
			
				
					// output //
Enter your name: John
Hello, John!

				
			

Explanation:

  • We declare a string variable name to store the user’s name.
  • We prompt the user to enter their name using std::cout.
  • We read the input using std::getline() function which reads the entire line of input including spaces.
  • Finally, we greet the user with the entered name using std::cout.

Substring Extraction

You can extract substrings from a string using the substr() function.

				
					#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    std::string substr = str.substr(7, 5); // Extracts "World"
    std::cout << substr << std::endl;
    return 0;
}

				
			
				
					// output //
World

				
			

Explanation:

  • We declare a string variable str and initialize it with the value "Hello, World!".
  • We use the substr() function to extract a substring starting from index 7 and of length 5.
  • The extracted substring "World" is stored in the variable substr, which we then print.

String Find

You can search for substrings within a string using the find() function.

				
					#include <iostream>
#include <string>

int main() {
    std::string str = "The quick brown fox jumps over the lazy dog.";
    size_t found = str.find("fox");
    if (found != std::string::npos)
        std::cout << "Substring found at index " << found << std::endl;
    else
        std::cout << "Substring not found!" << std::endl;
    return 0;
}

				
			
				
					// output //
Substring found at index 16

				
			

Explanation:

  • We declare a string variable str and initialize it with a sentence.
  • We use the find() function to search for the substring "fox" within the string.
  • If the substring is found, we print its starting index. Otherwise, we print a message indicating that the substring was not found.

String Comparison

You can compare two strings using the compare() function.

				
					#include <iostream>
#include <string>

int main() {
    std::string str1 = "apple";
    std::string str2 = "banana";

    if (str1.compare(str2) == 0)
        std::cout << "Strings are equal" << std::endl;
    else if (str1.compare(str2) < 0)
        std::cout << "String 1 is less than String 2" << std::endl;
    else
        std::cout << "String 1 is greater than String 2" << std::endl;

    return 0;
}

				
			
				
					// output //
String 1 is less than String 2

				
			

Explanation:

  • We declare two string variables str1 and str2 and initialize them with "apple" and "banana" respectively.
  • We use the compare() function to compare the two strings. If the result is 0, the strings are equal. If the result is negative, str1 is less than str2. If the result is positive, str1 is greater than str2.

String Insertion

You can insert a substring into a string using the insert() function.

				
					#include <iostream>
#include <string>

int main() {
    std::string str = "Hello!";
    str.insert(5, " World");
    std::cout << str << std::endl;
    return 0;
}

				
			
				
					// output //
Hello World!

				
			

Explanation:

  • We declare a string variable str and initialize it with the value "Hello!".
  • We use the insert() function to insert the substring " World" at index 5.
  • The resulting string is "Hello World!", which we then print.

String Erasure

You can erase a portion of a string using the erase() function.

				
					#include <iostream>
#include <string>

int main() {
    std::string str = "This is a sentence.";
    str.erase(5, 3); // Erase "is "
    std::cout << str << std::endl;
    return 0;
}

				
			
				
					// output //
This a sentence.

				
			

Explanation:

  • We declare a string variable str and initialize it with the value "This is a sentence.".
  • We use the erase() function to remove 3 characters starting from index 5.
  • The resulting string is "This a sentence.", which we then print.

we covered various aspects of string manipulation in C++. From basic operations like concatenation and length calculation to more advanced techniques such as substring extraction and string searching, you now have a comprehensive understanding of how to work with strings effectively in C++. Strings are a vital component of programming, and mastering their manipulation will greatly enhance your skills as a C++ developer. Experiment with the examples provided and explore further to deepen your understanding. Happy coding!Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India