0% found this document useful (0 votes)
2 views

Computer learning

The document provides an overview of manipulating strings in C++, detailing the string class, its constructors, member functions, and operators. It explains how to create and modify string objects, perform relational operations, and access individual characters. Key functions such as append(), insert(), replace(), and compare() are highlighted for string manipulation.

Uploaded by

hhcf2321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Computer learning

The document provides an overview of manipulating strings in C++, detailing the string class, its constructors, member functions, and operators. It explains how to create and modify string objects, perform relational operations, and access individual characters. Key functions such as append(), insert(), replace(), and compare() are highlighted for string manipulation.

Uploaded by

hhcf2321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Manipulation Strings in C++

Strings
String
Series of characters treated as single unit
Can include letters, digits, special characters +, -, * …
In c++ String object used like any other built-in data type
In C++ String is treated as another container class
For using the string class must include <string>
String class includes
Constructors
Member function
operators
Strings

 String class achieve the following


 Creating string objects
 Reading string objects from keyboard
 Displaying string object to the screen
 Finding a substring from a string
 Modifying, adding and comparing string objects
 Accessing characters in a string
 Obtaining the size of string
 …….
String Constructors

 String(); - for creating an empty string

 String(const char*s) - for creating string object from a null-


terminated string

 String(const string & str) - for creating string object from other
string object
String Class Functions

 Append() - appends a part of string to another string


 Assign() - assigns a partial string
 capacity - gives the total elements that can be used
 Compare() - compares the string against the invoking string
 Erase() - remove characters as specified
 insert() - inserts characters as specified
 length() - gives the number of elements in a string
 replace() - replace specified characters with a given string
 Resize() - changes the size of the string as specified
 Size() - gives the number of characters in the string
 Swap() - swaps the given string with the invoking string
Operators for string objects
operator
= Assignment
+ concatenation
+= Concatenation assignment
== Equality
!= Inequality
< Less than
<= Less than or equal
> greater than
>= greater than or equal
[] subscription
<< output

>> input
Creating (String) objects

 Can create string objects in a number of ways as:


 String s1; // using constructor with no parameter
 String s2 (“abc”); // using one-argument constructor
 S1=s2; // assigning string objects
 S3 = “abc” + s2 ; // concatenating strings
 Cin >> s1; // reding through keyboard
 Getline(cin, s2); // reding through keyboard a line of text
manipulating (String) objects

 We can easily modify the contents of string objects in several ways, such as
insert(), replace(), erase() and append().
 Ex:
S1 = 12345
S2 = abcde

S1.insert(4,s2); output s1 = 1234abcde5


S1.erase(4,5) output s1 = 1234
S2.replace(1,3,s1) output s2 = a12345e
Relational operations

 The relational operators are overloaded and can be used to compare


string objects.
compare()
s1.compare(s2)
 String characteristics
 The functions are size(), length(), capacity() etc…,
Str.size();
Str,length();
Str.capacity();
Str.max_size();
Accessing characters in strings

 The string class support following functions


 at() - for accessing individual charactes
 substr() - for retrieving a substring
 Find() - for finding a specified substring

 Comparing and swapping


 The compare() function used to compare either tow strings or portions of strings.
 The swap() function used for swapping the content of two string objects.
Ex:
s1.compare(s2);
s1.swap(s2);

You might also like