Class 11-12

Class 11 Computer Science

BSEB 100-Day Study Plan
Notes, MCQs, and Solutions

Explore Class 11

Class 12 Computer Science

Advanced Topics & Practical Guide
Board Exam Preparation

Explore Class 12

Day 43: Strings in C++ – Definition, Functions, and Examples | Class 11

Explore strings in C++ in Day 43 of Class 11 Computer Science. Covers string declaration, initialization, and commonly used string functions.

Day 43 – Chapter 5: Strings in C++

Class 11 Computer Science

What are Strings in C++? (C++ में Strings क्या हैं?)

A string in C++ is a sequence of characters stored in a contiguous memory location. Strings are used to store and manipulate text data.
C++ में एक String वर्णों का एक अनुक्रम है जो निरंतर मेमोरी स्थान में संग्रहीत होता है। Strings का उपयोग टेक्स्ट डेटा को संग्रहीत और हेरफेर करने के लिए किया जाता है।

Declaration and Initialization of Strings (Strings की घोषणा और प्रारंभ)

Strings can be declared in two ways: using character arrays or using the string class from the Standard Template Library (STL).
Strings को दो तरीकों से घोषित किया जा सकता है: कैरेक्टर एरे का उपयोग करके या स्टैंडर्ड टेम्पलेट लाइब्रेरी (STL) के string क्लास का उपयोग करके।

Using Character Arrays:

char str[20] = "Hello";
cout << str;

Using String Class:

#include <string>
string str = "Hello";
cout << str;

String Functions in C++ (C++ में String फ़ंक्शन्स)

C++ provides a variety of string functions to manipulate and process strings.
C++ स्ट्रिंग्स को हेरफेर और प्रोसेस करने के लिए विभिन्न प्रकार के फ़ंक्शन प्रदान करता है।

Function Description (विवरण) Example
length() Returns the length of the string.
String की लंबाई लौटाता है।
string str = "Hello";
cout << str.length(); // Output: 5
                
substr() Returns a substring starting at a specified index.
निर्दिष्ट इंडेक्स से एक सबस्ट्रिंग लौटाता है।
string str = "Hello";
cout << str.substr(1, 3); // Output: ell
                
append() Appends one string to another.
एक स्ट्रिंग को दूसरी से जोड़ता है।
string str1 = "Hello";
string str2 = " World";
cout << str1.append(str2); // Output: Hello World
                
compare() Compares two strings and returns 0 if they are equal.
दो Strings की तुलना करता है और समान होने पर 0 लौटाता है।
string str1 = "abc";
string str2 = "abc";
cout << str1.compare(str2); // Output: 0
                
find() Finds the first occurrence of a substring.
एक सबस्ट्रिंग की पहली उपस्थिति ढूंढता है।
string str = "Hello World";
cout << str.find("World"); // Output: 6
                

Examples of Using Strings (Strings के उपयोग के उदाहरण)

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str1 = "Hello";
    string str2 = "World";

    // String Concatenation
    string str3 = str1 + " " + str2;
    cout << "Concatenated String: " << str3 << endl;

    // Length of String
    cout << "Length of str3: " << str3.length() << endl;

    // Substring
    cout << "Substring: " << str3.substr(0, 5) << endl;

    // Finding a Word
    cout << "Position of 'World': " << str3.find("World") << endl;

    return 0;
}

Output:

Concatenated String: Hello World
Length of str3: 11
Substring: Hello
Position of 'World': 6

Practice Questions

Multiple Choice Questions (MCQs)

  1. Which header file is required for using the string class in C++?
    (a) string.h | (b) string | (c) iostream | (d) stdlib
  2. Which function returns the length of a string?
    (a) size() | (b) length() | (c) substr() | (d) None
  3. What does the append() function do?
    (a) Removes characters | (b) Appends one string to another | (c) Finds a substring | (d) None
  4. What is the output of str.substr(2, 3) if str = "Programming"?
    (a) Pro | (b) ram | (c) rog | (d) None
  5. Which function is used to find the first occurrence of a substring?
    (a) substr() | (b) find() | (c) compare() | (d) None
  6. What does the compare() function return if two strings are equal?
    (a) 0 | (b) 1 | (c) -1 | (d) None
  7. Which function extracts a substring?
    (a) substr() | (b) find() | (c) append() | (d) None
  8. What is the default value of a string in C++?
    (a) Empty String | (b) Null | (c) Undefined | (d) None
  9. Which function concatenates two strings?
    (a) substr() | (b) append() | (c) length() | (d) None
  10. What is the output of the following code?
    string str = "Hello";
    cout << str.length();
            
    (a) 4 | (b) 5 | (c) Error | (d) None

Answers to MCQs:

1: (b), 2: (b), 3: (b), 4: (b), 5: (b), 6: (a), 7: (a), 8: (a), 9: (b), 10: (b)

Short Answer Questions

  1. What is a string in C++?
    Answer: A string is a sequence of characters stored in a contiguous memory location.
  2. Write the syntax for declaring and initializing a string using the string class.
    Answer:
    #include <string>
    string str = "Hello";
    
  3. What is the use of the substr() function in C++?
    Answer: The substr() function extracts a substring from a given string starting from a specified index.
  4. How is the append() function used in C++?
    Answer: The append() function is used to concatenate one string to another.
  5. Write a program to concatenate two strings using the append() function.
    Answer:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string str1 = "Hello";
        string str2 = " World";
        cout << str1.append(str2);
        return 0;
    }
    

Long Answer Questions

  1. Explain strings in C++ and their importance with examples.
  2. Write a program to demonstrate string functions like length(), substr(), and find().
  3. Discuss the difference between character arrays and the string class in C++.

Post a Comment