ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP1112Strings And String Ope…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP1112

    Programming Fundamentals

    Progress0 / 19 done
    Topics19
    1Introduction to Problem Solving2Von-Neumann Architecture3Introduction to Programming4Role of Compiler and Linker5Introduction to Algorithms6Basic Data Types and Variables7Input/Output Constructs8Arithmetic, Comparison and Logical Operators9Conditional Statements and Execution Flow10Repetitive Statements and Execution Flow11Lists and Memory Organization12Multi-dimensional Lists13Introduction to Modular Programming14Function Definition and Calling15Stack Rolling and Unrolling16Strings and String Operations17Pointers/References18Static and Dynamic Memory Allocation19File I/O Operations
    3 min read
    Topic 16 of 19
    Topic Study Note Illustration
    16

    Strings and String Operations

    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...

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    3mremaining
    529words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals