Basic String Operations

C++ provides the std::string class for handling and manipulating strings. Here are some common operations you can perform with strings:

  1. Initialization:

    std::string str1 = "Hello";
    std::string str2("World");
    std::string str3;
    
    
  2. Concatenation:

    std::string str4 = str1 + " " + str2; // "Hello World"
    
    
  3. Accessing Characters:

    char c = str1[1]; // 'e'
    
    
  4. Length:

    size_t len = str1.length(); // 5
    
    
  5. Substrings:

    std::string substr = str1.substr(1, 3); // "ell"
    
    
  6. Finding Substrings:

    size_t pos = str1.find("lo"); // 3
    
    
  7. Comparison:

    if (str1 == "Hello") {
        // Do something
    }
    
    
  8. Modification:

    str1[0] = 'h'; // "hello"
    
    

Getline

getline is used to read an entire line of text from an input stream, which is useful for processing input that contains spaces.

Usage:

  1. Basic Usage:

    std::string input;
    std::getline(std::cin, input);
    
    
  2. With Custom Delimiter:

    std::string input;
    std::getline(std::cin, input, ','); // Reads until a comma is encountered
    
    
  3. For Loop Input:

    string s;
    int t;cin>>t;
    cin.ignore(); // to ignore the '\\n' from the first line after taking the input t;
    while(t--){
    	cin>>s;
    }
    

Handling Big Numbers

C++'s native data types (int, long, etc.) have size limitations, so handling very large numbers requires special techniques or libraries.

Using Strings to Represent Big Numbers

  1. Storing Big Numbers as Strings:

    std::string bigNum1 = "12345678901234567890";
    std::string bigNum2 = "98765432109876543210";