String Manipulations in C++

String Manipulations in C++

In the C++ programming language, the string data type is handled in two main ways. The C-style string character array in a C++ program is a string data type handling library, which we analyzed earlier. std::string is a built-in function or feature of the standard library in C++ programming. It provides a higher-level data information abstraction method for string data type methods in C++ programs.

String Manipulations in C++

In advanced C++ programming, the std::string class library is a desirable choice for string manipulation due to its ease, safety, and built-in functions. These features will primarily focus on handling and managing std::string in C++ programming.

Basic Operations with std::string in C++ Programming.

The std::string class library in C++ provides a variety of built-in functions and features for handling and managing strings. C++ users can perform multiple string character operations using these class library functions.

So, let’s take a closer look at some common std::string class library operations.

Declaring and initializing a std::string in C++.

C++ users can declare and initialize the desired string character data type element in the std::string class library using these methods.

Example of Declaring and initializing a std::string.

#include <iostream>

#include <string> // Here we use std::string for string data type operations in C++

int main() {

std::string text1 = “Vcanhelpsu, Edtech”; // Here we declare a string literal data type

std::string text2 = text1; // Here we copy another string into

std::string text3 = text1;

std::cout << text1 << std::endl; // Result is – Vcanhelpsu, Edtech

std::cout << text2 << std::endl; // Result is – Vcanhelpsu, Edtech

std::cout << text3 << std::endl; // Result is – Vcanhelpsu, Edtech

return 0;

}

std::string class library basic operations in C++ program.

String length (size() or length()) concept of the std::string class library.

C++ users can find the current length of a std::string by applying the size() or length() built-in string function in a program. In this, we can analyze both the user-defined and declared string data type element equality sizes.

Example of String length (size() or length()).

#include <iostream>

#include <string>

int main() {

std::string c_name = “Vcanhelpsu Edtech”;

std::cout << “Size of company name text – ” << c_name.size() << std::endl; // Result – 17

std::cout << “Length of company name string – ” << c_name.length() << std::endl; // Result – 17

return 0;

}

Accessing characters is a concept of the std::string class library.

C++ users can access and display individual characters in a user-declared string data type by indexing or applying the at() function. The at() function is more efficient and secure in a C++ program because it checks user-defined string bounds.

Example of accessing characters concept of the std::string class library.

#include <iostream>

#include <string>

int main() {

std::string c_name = “Vcanhelpsu”;

// Here we access the character of the string using the storage element indexing method

std::cout << “First character of string – ” << c_name[0] << std::endl; // Result is – V

// Here we access the character of the string using the at() function bounds checked method

std::cout << “Second character of string – ” << c_name.at(1) << std::endl; // Result is – c

return 0;

}

Modifying a string concept of the std::string class library.

C++ users can modify a string in a program by assigning a new value to it, adding a new element to an existing string, or updating or replacing characters in an existing string.

Assigning a new string.

std::string c_name = “Vcanhelpsu”;

c_name = “Edtech”; // Now the c_name string variable holds the text value “Edtech”.

Adding string elements using the += or append() function.

std::string c_name = “Vcanhelpsu”;

c_name += “Edtech”; //Add using the `+=` operator.

std::cout << c_name << std::endl; // Result – Vcanhelpsu Edtech

c_name.append(“#”);

std::cout << c_name << std::endl; // Result – Vcanhelpsu Edtech#

Replacing string characters at a specific position.

std::string c_name = “Vcanhelpsu”;

c_name[0] = ‘V’; // Displays the first character here as ‘V’.

std::cout << c_name << std::endl; // Result – Vcanhelpsu

Replacing a substring in the std::string class library using the replace() function.

#include <iostream>

#include <string>

int main() {

std::string text = “Vcanhelpsu, Platform”;

// here we Replace substring index 10 to 11 with a new user defined string

text.replace(10, 11, “Edtech”);

std::cout << text << std::endl; // Result – Vcanhelpsu Edtech

return 0;

}

String concatenation is a concept of the std::string class library.

C++ users can concatenate two different strings of text data by applying the + operator or the append() built-in function method in a program.

Example of String concatenation is a concept of the std::string class library.

#include <iostream>

#include <string>

int main() {

std::string text1 = “Vcanhelpsu”;

std::string text2 = “Edtech”;

// Here it concatenates text with the ‘+’ operator to add two different strings.

std::string output = text1 + ” ” + text2;

std::cout << output << std::endl; // Result – Vcanhelpsu Edtech

// Here it concatenates text with the ‘append()’ operator to add two different strings.

text1.append(” “).append(text2);

std::cout << text1 << std::endl; // Result – Vcanhelpsu Edtech

return 0;

}

String comparison concept of the std::string class library.

C++ users can compare two different strings in a program by applying comparison operators or the compare() built-in function method.

Example of String comparison concept of the std::string class library.

#include <iostream>

#include <string>

int main() {

std::string text1 = “Bhavishi”;

std::string text2 = “Siddhi”;

if (text1 == text2) {

std::cout << “Both text strings are equal.” << std::endl;

} else {

std::cout << “Both text strings are different.” << std::endl; // Result – Both text strings are different.

}

return 0;

}

The compare() function is a concept of the std::string class library.

  • The compare() function provides some built-in features for C++ user programs.
  • If both user-defined text strings are equal, the output is 0.
  • If the user-defined first string is lexicographically smaller than the second string, it is itself a negative value.
  • If the user-defined first string is lexicographically larger than the second, it is a positive value.

Example of the compare() function concept.

#include <iostream>

#include <string>

int main() {

std::string text1 = “Java”;

std::string text2 = “Python”;

if (text1.compare(text2) == 0) {

std::cout << “Here both text strings are equal in nature.” << std::endl;

} else if (text1.compare(text2) < 0) {

std::cout << “Here text1 is lexicographically lower than text2.” << std::endl;

} else {

std::cout << “Here text1 is lexicographically higher than text2.” << std::endl;

}

return 0;

}

Substring concept of the std::string class library.

C++ users can extract a smaller substring from a larger text string by applying the substr() function to a program.

You must have two required parameters for this.

  • The starting string position (index).
  • The length of the current substring (optional).

Example of Substring is a concept.

#include <iostream>

#include <string>

int main() {

std::string c_name = “Vcanhelpsu, Edtech”;

// Extract the substring text starting from position 11 with length 8

std::string subtext = c_name.substr(11, 8);

std::cout << “Here is the substring text of c_name – ” << subtext << std::endl; // Result – Here is the substring text of c_name – Edtech

return 0;

}

String search is a concept of the std::string class library.

C++ users can search for a substring or character in an existing string by applying the find() or rfind() function methods to a program.

Example of finding a character or substring.

#include <iostream>

#include <string>

int main() {

std::string c_name = “Vcanhelpsu,Edtech”;

// here we Find the first text string occurrence of ‘p’

size_t loc = c_name.find(‘p’);

if (loc != std::string::npos) {

std::cout << “‘p’ character found at location – ” << loc << std::endl; // Result – 7

} else {

std::cout << “‘p’ character not found -” << std::endl;

}

// here it Find a substring in complete above string

loc = c_name.find(“Edtech”);

if (loc != std::string::npos) {

std::cout << “‘Edtech’ word found at location – ” << loc << std::endl; // Result – 12

}

return 0;

}

Explanation of finding a character or substring.

  • In this program, the find() function indicates the current position location of the first occurrence of the current substring or character.
  • In this example, npos is a constant that displays the information “‘Edtech’ word found at location” in the current program.

Finding the last occurrence (rfind()) function.

#include <iostream>

#include <string>

int main() {

std::string text = “Java, Python Matlab”;

// Find the last occurrence of the “Matlab” text string

size_t loc = text.rfind(“Matlab”);

if (loc != std::string::npos) {

std::cout << “Here ‘Matlab’ text found at location – ” << loc << std::endl; // Result – 13

}

return 0;

}

String input/output concept of the std::string class library.

C++ users can use the std::cin and std::cout statement methods to read and print an existing text string in a program.

Example of reading a String input/output.

#include <iostream>

#include <string>

int main() {

std::string message;

std::cout << “Enter a desired text message – “;

std::getline(std::cin, message); // here it reads the entire text line, including white spaces

std::cout << “Your input text message is – ” << message << std::endl;

return 0;

}

Explanation of reading a String input/output.

  • Here in this program, the std::getline() library function reads the entire text line, including white spaces.
  • Here, std::cin >> message; It reads input up to the first empty space entered by the user. It cannot be used for text strings with too many spaces.

Leave a Reply