(Type The Company Name) - LAB NO 03

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

[Type the company name] | LAB NO 03 1

LAB No.




Name.
Program.
Department.
Subject.
Lab. Topic.
Submit Date. .





[Type the company name] | LAB NO 03 2



STRI NG I N C++


The term string generally means an ordered sequence of characters, with a first
character, a second character, and so on, and in most programming languages such
strings are enclosed in either single or double quotes. In C++ the enclosing delimiters
are double quotes. In this form the string is referred to as a string literal and we often
use such string literals in output statements when we wish to display text on the screen
for the benefit of our users. For example, the usual first C++ program displays the string
literal "Hello, world!" on the screen with the following output statement:
cout << "Hello, world!" << endl;

A C++ string object, which is an instance of a "class" data type whose actual
internal representation you need not know or care about, as long as you know what
you can and can't do with variables (and constants) having this data type. There is a
library of C++ string functions as well, available by including the <string> header file.
Standard C++ includes a new class called string. This class improves on the
traditional C string in many ways. For one thing, you no longer need to worry about
creating an array of the right size to hold string variables. The string class assumes
all the responsibility for memory management. Also, the string class allows the
use of overloaded operators, so you can concatenate string objects with the + operator:
s3 = s1 + s2


with the + operator C++ String for better understanding.


C++ strings (#include <string>)

Declaring a C++ string object
string str;






[Type the company name] | LAB NO 03 3





Initializing a C++ string object
string str1("Call home!");
string str2 = "Send money!";
string str3("OK");

Assigning to a C++ string object
string str;
str = "Hello";
str = otherString;

Concatenating two C++ string objects
str1 += str2;
str = str1 + str2;

Copying a C++ string object
string str;
str = "Hello";
str = otherString;

Accessing a single character
str[index]
str.at(index)
str(index, count)

Comparing two C++ string objects
if (str1 < str2)
cout << "str1 comes 1st.";
if (str1 == str2)
cout << "Equal strings.";
if (str1 > str2)
cout << "str2 comes 1st.";

Finding the length of a C++ string object
str.length()

Output of a C++ string object
cout << str;
cout << setw(width) << str;




[Type the company name] | LAB NO 03 4



In what follows, keep in mind that cin ignores white space when reading string,
while cin.get(), cin.getline() and getline() donot.Remembertoothat cin.getline() and getline(
)consume the delimiter while cin.get() does not. Finally, cin can be replaced with any open
input stream, since file input with inFile, say, behaves in a manner completely analogous to
the corresponding behavior of cin. Analogously, in the output examples given immediately
above, cout could be replaced with any text output stream variable, say outFile. In all
cases, numCh is the maximum number of characters that will be read.

Input of a C++ string object
cin >> s;
getline(cin, s);
getline(cin, s, 'x');

You might also like