EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  C++ for EmSAT Achieve  >  C++ string class and its applications

C++ string class and its applications | C++ for EmSAT Achieve PDF Download

Introduction

When working with text and manipulating strings in C++, the string class is a powerful tool that provides a wide range of functionalities. It is part of the Standard Template Library (STL) and simplifies string handling, making it easier to work with and manipulate text data. In this article, we will explore the string class in C++ and its various applications.

Creating and Initializing Strings

To start using the string class, we first need to include the '<string>' header in our code. Here's an example of how to create and initialize a string variable:

#include <iostream>

#include <string>


int main() {

    std::string greeting = "Hello, world!";

    std::cout << greeting << std::endl;


    return 0;

}

Output

Hello, world!

In the above code, we include the necessary headers and declare a string variable called 'greeting'. We initialize it with the text "Hello, world!" and then output the string using 'std::cout'.

String Concatenation

One of the most common operations with strings is concatenation, which involves combining two or more strings into a single string. In C++, we can use the '+' operator or the '+=' operator to concatenate strings. Let's see an example:

#include <iostream>

#include <string>


int main() {

    std::string firstName = "John";

    std::string lastName = "Doe";

    std::string fullName = firstName + " " + lastName;


    std::cout << "Full Name: " << fullName << std::endl;


    return 0;

}

Output

Full Name: John Doe

In the above code, we declare three string variables: 'firstName', 'lastName', and 'fullName'. We concatenate the first name, a space, and the last name using the '+' operator and assign the result to the 'fullName' variable.

String Length

To determine the length of a string, we can use the 'length()' or 'size()' member functions of the string class. Here's an example:

#include <iostream>

#include <string>


int main() {

    std::string message = "Hello, world!";

    int length = message.length();


    std::cout << "Length: " << length << std::endl;


    return 0;

}

Output

Length: 13

In the above code, we declare a string variable 'message' and assign it the value "Hello, world!". We then use the 'length()' function to get the length of the string and store it in the 'length' variable.

String Comparison

To compare two strings in C++, we can use the '==' operator, which returns 'true' if the strings are equal and 'false' otherwise. Let's see an example:

#include <iostream>

#include <string>


int main() {

    std::string password = "password123";

    std::string input;


    std::cout << "Enter the password: ";

    std::cin >> input;


    if (input == password) {

        std::cout << "Access granted!" << std::endl;

    } else {

        std::cout << "Access denied!" << std::endl;

    }


    return 0;

}

In the above code, we declare a string variable 'password' and initialize it with the value "password123". We then prompt the user to enter a password and store it in the input variable. Using the '==' operator, we compare the user input with the password and display the appropriate message.

String Substring

Sometimes, we need to extract a portion of a string, known as a substring. The string class provides a 'substr()' member function that allows us to do just that. Here's an example:

#include <iostream>

#include <string>


int main() {

    std::string sentence = "The quick brown fox";

    std::string substring = sentence.substr(4, 5);


    std::cout << "Substring: " << substring << std::endl;


    return 0;

}

Output

Substring: quick

In the above code, we declare a string variable 'sentence' and assign it the value "The quick brown fox". We then use the 'substr()' function to extract a substring starting at index 4 and consisting of 5 characters.

Sample Problems

1. Write a program that takes a string as input and counts the number of vowels (a, e, i, o, u) in the string.

#include <iostream>

#include <string>


int main() {

    std::string input;

    std::cout << "Enter a string: ";

    std::cin >> input;


    int vowelCount = 0;

    std::string vowels = "aeiou";


    for (char c : input) {

        if (vowels.find(tolower(c)) != std::string::npos) {

            vowelCount++;

        }

    }


    std::cout << "Number of vowels: " << vowelCount << std::endl;


    return 0;

}

2. Write a program that reverses a given string and prints the reversed string.

#include <iostream>

#include <string>


int main() {

    std::string input;

    std::cout << "Enter a string: ";

    std::cin >> input;


    std::string reversed = "";

    for (int i = input.length() - 1; i >= 0; i--) {

        reversed += input[i];

    }


    std::cout << "Reversed string: " << reversed << std::endl;


    return 0;

}

Conclusion

In this article, we have explored the string class in C++ and its various applications. We have seen how to create and initialize strings, perform concatenation, determine the length of a string, compare strings, and extract substrings. The string class provides a rich set of functions and makes working with text data in C++ much easier. By understanding and utilizing these string operations, you can efficiently handle and manipulate strings in your C++ programs.

The document C++ string class and its applications | C++ for EmSAT Achieve is a part of the EmSAT Achieve Course C++ for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
70 videos|45 docs|15 tests

Top Courses for EmSAT Achieve

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Previous Year Questions with Solutions

,

Semester Notes

,

C++ string class and its applications | C++ for EmSAT Achieve

,

study material

,

pdf

,

shortcuts and tricks

,

ppt

,

past year papers

,

Important questions

,

C++ string class and its applications | C++ for EmSAT Achieve

,

Viva Questions

,

MCQs

,

mock tests for examination

,

video lectures

,

Sample Paper

,

Objective type Questions

,

Free

,

Extra Questions

,

C++ string class and its applications | C++ for EmSAT Achieve

,

practice quizzes

,

Summary

,

Exam

;