C++ Tutorial

Class and Object

Reference

Inheritance and Derivation

Polymorphism and Virtual Functions

Operator Overloading

Template

Exception

Object Oriented Advanced

Input/Output Stream

File Operations

String in C++

In this tutorial, we will learn about strings in C++. Strings are a fundamental data type in programming, used to represent text data. In C++, the standard library provides the std::string class, which is part of the <string> header, to handle strings more efficiently and easily compared to character arrays.

  • Include the necessary headers:

First, include the necessary headers for the program:

#include <iostream>
#include <string>
  • Creating and initializing strings:

You can create and initialize strings in several ways:

// Create an empty string
std::string str1;

// Initialize a string with a string literal
std::string str2("Hello, World!");

// Initialize a string with a character array
char arr[] = "Hello, World!";
std::string str3(arr);

// Initialize a string with a part of a character array
char arr2[] = "Hello, World!";
std::string str4(arr2, 5); // Initialize with the first 5 characters: "Hello"

// Initialize a string with a specific number of characters
std::string str5(5, 'A'); // Initialize with 5 'A' characters: "AAAAA"
  • Basic string operations:

The std::string class provides many useful functions and operators to manipulate and access strings:

  • Concatenation:

You can concatenate strings using the + operator or the append() function:

std::string s1 = "Hello";
std::string s2 = "World";

std::string s3 = s1 + " " + s2; // "Hello World"
s1.append(s2); // s1 now contains "HelloWorld"
  • Comparison:

You can compare strings using the comparison operators (==, !=, <, >, <=, >=):

std::string s1 = "apple";
std::string s2 = "banana";

if (s1 == s2) {
    std::cout << "Strings are equal" << std::endl;
} else if (s1 < s2) {
    std::cout << "s1 is less than s2" << std::endl;
} else {
    std::cout << "s1 is greater than s2" << std::endl;
}
  • Length and capacity:

You can get the length of a string using the length() or size() functions and check if a string is empty with the empty() function:

std::string s1 = "Hello";
std::cout << "Length of s1: " << s1.length() << std::endl; // Output: Length of s1: 5

if (s1.empty()) {
    std::cout << "s1 is empty" << std::endl;
} else {
    std::cout << "s1 is not empty" << std::endl; // Output: s1 is not empty
}
  • Accessing characters:

You can access individual characters in a string using the [] operator or the at() function:

std::string s1 = "Hello";

char firstChar = s1[0]; // 'H'
char secondChar = s1.at(1); // 'e'
  • Modifying characters:

You can modify characters in a string using the [] operator or the at() function:

std::string s1 = "Hello";
s1[0] = 'h'; // s1 now contains "hello"
s1.at(4) = 'O'; // s1 now contains "hellO"
  1. How to declare and initialize strings in C++:

    #include <iostream>
    #include <string>
    
    int main() {
        // Declaration and initialization of strings
        std::string str1 = "Hello, ";
        std::string str2("world!");
    
        // Concatenation of strings
        std::string result = str1 + str2;
    
        // Output the result
        std::cout << result << std::endl;
    
        return 0;
    }
    
  2. String manipulation functions in C++:

    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
    
        // String manipulation functions
        std::cout << "Length: " << str.length() << std::endl;
        std::cout << "Substr(7, 5): " << str.substr(7, 5) << std::endl;
        std::cout << "Find 'world': " << str.find("world") << std::endl;
        std::cout << "Replace 'world' with 'C++': " << str.replace(str.find("world"), 5, "C++") << std::endl;
    
        return 0;
    }
    
  3. String concatenation and appending in C++:

    #include <iostream>
    #include <string>
    
    int main() {
        std::string str1 = "Hello, ";
        std::string str2 = "world!";
    
        // Concatenation and appending
        str1 += str2;
        std::cout << str1 << std::endl;
    
        return 0;
    }
    
  4. Comparing strings in C++:

    #include <iostream>
    #include <string>
    
    int main() {
        std::string str1 = "Hello";
        std::string str2 = "World";
        std::string str3 = "Hello";
    
        // Comparing strings
        if (str1 == str2) {
            std::cout << "str1 and str2 are equal." << std::endl;
        } else {
            std::cout << "str1 and str2 are not equal." << std::endl;
        }
    
        if (str1 == str3) {
            std::cout << "str1 and str3 are equal." << std::endl;
        } else {
            std::cout << "str1 and str3 are not equal." << std::endl;
        }
    
        return 0;
    }
    
  5. String input and output in C++:

    #include <iostream>
    #include <string>
    
    int main() {
        std::string inputStr;
    
        // Input string
        std::cout << "Enter a string: ";
        std::getline(std::cin, inputStr);
    
        // Output string
        std::cout << "You entered: " << inputStr << std::endl;
    
        return 0;
    }
    
  6. String length and size in C++:

    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
    
        // String length and size
        std::cout << "Length: " << str.length() << std::endl;
        std::cout << "Size: " << str.size() << std::endl;
    
        return 0;
    }
    
  7. Substring extraction in C++:

    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
    
        // Substring extraction
        std::string subStr = str.substr(7, 5);
    
        // Output the substring
        std::cout << "Substring: " << subStr << std::endl;
    
        return 0;
    }
    
  8. Searching for substrings in C++ strings:

    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
    
        // Searching for substrings
        size_t found = str.find("world");
    
        if (found != std::string::npos) {
            std::cout << "Substring found at position " << found << std::endl;
        } else {
            std::cout << "Substring not found." << std::endl;
        }
    
        return 0;
    }
    
  9. Modifying and replacing characters in C++ strings:

    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
    
        // Modifying and replacing characters
        str[7] = 'W';
        std::cout << "Modified string: " << str << std::endl;
    
        str.replace(7, 5, "C++");
        std::cout << "Replaced string: " << str << std::endl;
    
        return 0;
    }
    
  10. String conversion to numeric types in C++:

    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "42";
        
        // String conversion to numeric types
        int intValue = std::stoi(str);
        double doubleValue = std::stod(str);
    
        std::cout << "Integer value: " << intValue << std::endl;
        std::cout << "Double value: " << doubleValue << std::endl;
    
        return 0;
    }
    
  11. Converting numeric types to strings in C++:

    #include <iostream>
    #include <string>
    
    int main() {
        int intValue = 42;
        double doubleValue = 3.14;
    
        // Converting numeric types to strings
        std::string strInt = std::to_string(intValue);
        std::string strDouble = std::to_string(doubleValue);
    
        std::cout << "String from int: " << strInt << std::endl;
        std::cout << "String from double: " << strDouble << std::endl;
    
        return 0;
    }
    
  12. C++ string class and its member functions:

    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "C++ String";
    
        // String class member functions
        std::cout << "Length: " << str.length() << std::endl;
        std::cout << "Front: " << str.front() << std::endl;
        std::cout << "Back: " << str.back() << std::endl;
        std::cout << "Empty: " << (str.empty() ? "true" : "false") << std::endl;
    
        return 0;
    }