ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    COMP1112
    Progress0 / 19 topics
    Topics
    1. Introduction to Problem Solving2. Von-Neumann Architecture3. Introduction to Programming4. Role of Compiler and Linker5. Introduction to Algorithms6. Basic Data Types and Variables7. Input/Output Constructs8. Arithmetic, Comparison and Logical Operators9. Conditional Statements and Execution Flow10. Repetitive Statements and Execution Flow11. Lists and Memory Organization12. Multi-dimensional Lists13. Introduction to Modular Programming14. Function Definition and Calling15. Stack Rolling and Unrolling16. Strings and String Operations17. Pointers/References18. Static and Dynamic Memory Allocation19. File I/O Operations
    COMP1112›Strings and String Operations
    Programming FundamentalsTopic 16 of 19

    Strings and String Operations

    3 minread
    529words
    Beginnerlevel

    Strings and String Operations in C++

    Strings are fundamental data types in C++ that represent sequences of characters. They are widely used for handling text data, and C++ provides a rich set of operations to manipulate strings efficiently.

    1. String Representation

    In C++, strings can be represented in two main ways:

    1. C-style Strings: These are arrays of characters terminated by a null character ('\0').

      char str[] = "Hello, World!";
      
    2. C++ Standard Library Strings: The std::string class, part of the C++ Standard Library, provides a more flexible and easier-to-use string representation.

      #include <string>
      std::string str = "Hello, World!";
      

    2. Common String Operations

    2.1. Initialization and Assignment

    C-style Strings:

    char str1[] = "Hello";
    char str2[6];
    strcpy(str2, str1); // Copying str1 to str2
    

    C++ Strings:

    #include <string>
    std::string str1 = "Hello";
    std::string str2 = str1; // Copy assignment
    
    2.2. Concatenation

    C-style Strings:

    char str1[20] = "Hello, ";
    char str2[] = "World!";
    strcat(str1, str2); // str1 now contains "Hello, World!"
    

    C++ Strings:

    std::string str1 = "Hello, ";
    std::string str2 = "World!";
    std::string result = str1 + str2; // Concatenation
    
    2.3. Length

    C-style Strings:

    int length = strlen(str1); // Returns the length of str1
    

    C++ Strings:

    std::string str = "Hello, World!";
    size_t length = str.length(); // Returns the length of str
    
    2.4. Accessing Characters

    C-style Strings:

    char firstChar = str1[0]; // Access first character
    

    C++ Strings:

    char firstChar = str[0]; // Access first character
    char anotherChar = str.at(1); // Access second character with bounds checking
    
    2.5. Substrings

    C++ Strings:

    std::string str = "Hello, World!";
    std::string sub = str.substr(7, 5); // Extracts "World"
    
    2.6. Finding Substrings

    C++ Strings:

    size_t position = str.find("World"); // Returns the starting index of "World"
    if (position != std::string::npos) {
        // Found
    }
    
    2.7. String Comparison

    C-style Strings:

    if (strcmp(str1, str2) == 0) {
        // Strings are equal
    }
    

    C++ Strings:

    if (str1 == str2) {
        // Strings are equal
    }
    
    2.8. Replacing Substrings

    C++ Strings:

    std::string str = "Hello, World!";
    str.replace(7, 5, "C++"); // Replaces "World" with "C++"
    
    2.9. String Iteration

    C++ Strings:

    for (char c : str) {
        std::cout << c; // Iterate over each character
    }
    

    3. String Input/Output

    You can use standard input and output streams to work with strings.

    Input:

    std::string input;
    std::cout << "Enter a string: ";
    std::getline(std::cin, input); // Read entire line including spaces
    

    Output:

    std::cout << "You entered: " << input << std::endl;
    

    Summary

    Strings in C++ are powerful and flexible tools for handling text data. The std::string class provides a variety of operations for manipulation, including concatenation, searching, and modification. Understanding how to use strings and their associated operations is essential for effective programming in C++. Whether using C-style strings or the more convenient std::string, mastering these operations will enhance your ability to handle textual data in your applications.

    Previous topic 15
    Stack Rolling and Unrolling
    Next topic 17
    Pointers/References

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time3 min
      Word count529
      Code examples0
      DifficultyBeginner