Strings in c plus plus

NaveedJamali1 126 views 9 slides Aug 10, 2021
Slide 1
Slide 1 of 9
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9

About This Presentation

Strings in C++,
The string is a data type used in CPP to store text in the memory. In these slides, we will see how to use String datatype in a c++ program.
we will include the string header file, declare string variables, and then do operations on these variables.


Slide Content

Strings in c++ Created by: ________________ Instructor: __________________

WELCOME

What is string Like int , double , char , and bool , “String” is a datatype. Strings are used for storing text (names, sentences, phrases, etc). For example: string name = “Pakistan”;

How String is used A string contains a collection of characters, surrounded by double quotes Example: string greeting =  " Hello " ;

Using String To use string, you must include <string> header file in the code Example // Include the string library #include <string> // Create a string variable string greeting = "Hello";

Some String examples String firstName = “John”; String lastName = “Doe”; // Combining two strings in c++: String fullName = firstName + lastName;

Get string from user #include <iostream> #include <string> using namespace std; int main() { string fullName; cout << "Type your full name: "; getline (cin, fullName); cout << "Your name is: " << fullName; return 0; }

C++ String length To check how many characters are stored in a string, we use length() function of string. Example: string alphabet =  "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; cout <<  "The length of the txt string is: "  << alphabet .length() ; //OUTPUT of code: //The length of the txt string is: 26

Thank you Created by: ___________ Instructor: _________________