delete one specific character in string C++

To delete a specific character in a C++ string, you can use the erase() function from the string library. The erase() function allows you to remove a range of characters from a string, and you can specify the position of the character you want to delete.

Here are the steps to delete a specific character in a string in C++:

  1. Include the string library at the beginning of your code to use the string class.

cpp #include <string>

  1. Declare a string variable and assign a value to it.

cpp std::string str = "Hello World";

  1. Determine the position of the character you want to delete. You can use the find() function to find the position of the character in the string.

cpp size_t pos = str.find('o');

  1. Use the erase() function to delete the character at the specified position. Pass the position and the number of characters to erase as arguments.

cpp str.erase(pos, 1);

  1. Print the modified string to verify that the character has been deleted.

cpp std::cout << str << std::endl;

The complete code would look like this:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello World";
    size_t pos = str.find('o');
    str.erase(pos, 1);
    std::cout << str << std::endl;
    return 0;
}

When you run this code, it will output "Hell World", with the character 'o' removed from the original string.