0% found this document useful (0 votes)
56 views27 pages

Chapter 4 - Strings

The document discusses string operations in C++. It describes what strings are, how to initialize and input strings, and various operators and functions to manipulate strings like concatenation, comparison, substring extraction, searching, and modifying strings by appending, inserting, erasing, or swapping characters and substrings.

Uploaded by

klpxsnest1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views27 pages

Chapter 4 - Strings

The document discusses string operations in C++. It describes what strings are, how to initialize and input strings, and various operators and functions to manipulate strings like concatenation, comparison, substring extraction, searching, and modifying strings by appending, inserting, erasing, or swapping characters and substrings.

Uploaded by

klpxsnest1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

CHAPTER 4

Accessing different kind of data:


STRINGS
4.3.1 What is a string?
 A string is a set of characters.
 The string type is not a built-in type so when we want
to use it, we have to put the #include<string>
directive.
4.3.2 Initializing a string
• A string can be initialized as:
1) String literal

2) pair of parentheses and an argument

3) use a value stored in any string variable


4.3.5 String operators: +
• the string type has its own operators.
• One of the most important and most frequently used is
the + operator.
• It doesn’t add strings in the same way as traditional
arithmetic addition. Instead, it concatenates strings
• the concatenating + is not commutative (a + b aren’t
always equal to b + a) in contrast to the adding +.
4.3.5 String operators: +
• It cannot concatenate literals.
• It can concatenate
• any variable with a literal,
• a literal with a variable,
• And a variable with another variable,

Syntax Error
4.3.7 String operators: +=

To be or not to be
4.3.8 Inputting strings
• cin stream treats spaces as delimiters, demarcating limits
between data

To be or not to be
To
4.3.8 Inputting strings
• If you want to input a whole line of text and treat the
white characters just like any other character, you have to
use the getline function.

To be or not to be
To be or not to be
4.3.10 Comparing strings
• you can compare two strings .
• All the operators designed to compare data are at your
disposal: > < >= <= !=.
4.3.10 Comparing strings
• Using the compare function
• str1.compare(str2) == 0 when str1 == str2
• str1.compare(str2) == 1 when str1 > str2
• str1.compare(str2) == -1 when str1 < str2
4.4.1 string length and empty functions
• To obtain the length of a string object x, call the method
length() or size():

int len = str.length( );


OR
int len = str.size( );

• To check of x is empty (that is, has no characters in it):

bool y = str.empty();
15
Example 1 0
Not Empty string
Empty string
• What is the output?
4.4.2 Substrings
•A substring of a string x is a subsequence of consecutive characters
in x
•For example, “rod” is a substring of “product”

x.substr(pos,len);

•substr returns a substring without modifying the string to which


the function was applied.

•The default value for position is 0


•The default value of the length is str.length()
Example 1

string strA = "123456789";


strB = strA.substr(2,3); // value is "345"
strC = strA.substr(2); // value is "3456789“
strD = strA.substr(); //value is “123456789"
4.4.3 compare
• To compare between two string for equality
• S.compare(substr_start, substr_length,
other_string)

string S = "ABC";
cout << S.compare(1,1,"B");

Will print 0
4.4.3 compare
• S.compare(substr_start, substr_length,
other_string, other_substr_start,
other_substr_length)

string S = "ABC";
cout << S.compare(1,1,"ABC",1,1);

Will print 0
4.4.4 Finding strings inside strings
• Suppose x is a string object, and suppose you want to
search for a string y in x.
• To do so, write:

int startLoc = x.find(y);


• This method returns the starting index of the leftmost
occurrence of y in x, if any occurrence exists;
• otherwise, the method returns the maximum size of a
string (string::npos)
• To search starting from a position pos, do

int startLoc = x.find(y, pos);


Example
• Write a program that enter a line and count how many
word in it. also, print each word in a separate line
4.4.8 How to control the content of the string
• We can process the string as an array of chars. The last
character of any string is NULL (‘\0’).

• Example:
• enter a string and print each char in a separate line
4.5.1 Appending a (sub)string
• Append function it’s designed to append one string to
another
• Examples:
• string str1 = "content“, str2 =
"appendix"; str1.append(str2);
• // str1 contains "contentappendix" now

• string str1 ="content“,str2 =


"appendix";
str1.append(str2,0,3);
• // str1 contains "contentapp" now
4.5.1 Appending a (sub)string
• the same task can be performed by the += operator
• The append function is much more flexible and helpful
than += and can give you more options.

• string str1 = "content";


str1.append(3, 'x');
• // str1 contains "contentxxx" now
4.5.2 Appending a character
• If you want to append just one character to a string, you
can do it by using the append function, but there’s a
more efficient way, by using the push_back member
function.
4.5.3 Inserting a (sub)string or a character
• Suppose x is a string object, and let y be another string
to be inserted at position pos of the string of x
• To insert y, do:

x.insert(pos,y);

• Example

string quote = "to be ";


quote.append(quote);
quote.insert(6, "or not ");
cout << quote << endl;
to be or not to be
4.5.4 Assigning a (sub)string or a character
• The assign member function does a job which is very
similar to the insert’s job, but does not retain the
previous string content, and instead just replaces it with
a new one.
4.5.6 Erasing a (sub)string
• We can also remove a part of a string, making the string
shorter than before
• Examples:
string s = “C++ Programming”;
s.erase(4,5); //erase from position 4, 5
characters
//s will contain “C++ amming”
s.erase(3); //erase from position 2 to the
end of the string
//s will contain “C++”
s.erase();// erase the string
//s will be empty
4.5.7 Exchanging the contents of two strings
• Using swap function

A martini. Shaken, not stirred.


Shaken, not stirred. A martini.

You might also like