Introduction
• A stringis defined as a sequence of characters. In C, we have used null-terminated
character arrays to store and manipulate strings. These strings are often called C-Strings
or C-Style Strings.
• Contrary to popular belief, there is no string data type in C++.
• C++ being an advanced superset of C, provides a new class called string, which
improves on the conventional C-Strings in many ways.
• String is not a fundamental data type like int, float or char, but rather a complex data
structure which comprises of basic data elements.
4.
C-Style Strings
• TheC-style character string originated within the C language and continues to be
supported within C++.
• This string is actually a one-dimensional array of characters which is terminated by
a null character '0’. Thus, a null-terminated string contains the characters that comprise
the string followed by a null.
• Examples:
char greeting[6] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘0’};
char greeting[6] = “Hello”;
5.
Strings in C++
•A string is a class that defines objects to be represented as a stream of characters. The
string class includes many inbuilt member functions, constructors and destructors that
allow many operations on strings unlike character arrays.
• We can create string objects in a number of ways as shown below:
string s1; // Declaring string object
string s2 = “xyz”; // Initializing during run time
cin >> s1; // Reading through keyboard
getline (cin, s1); // Reading a line of text through keyboard
6.
Program Example
#include <iostream>
usingnamespace std;
int main( )
{
string s1 = "Hello";
char ch[] = { 'C', '+', '+'};
string s2 = string(ch);
cout<<s1<<endl;
cout<<s2<<endl;
}
OUTPUT:
Hello
C++
7.
String Functions
• Amajor difference between the string functions in C and C++ is that in C++, the
functions are part of the class String, whereas in C, they are generally included in the
header file <string.h>.
• Many of the string functions such as strlen, strcat, strcpy, strchr and strcmp are
retained from C string library.
• However, some functions have been improved upon in syntax and are easier to use.
• The next slides lists some of the functions and operators used in relation with strings in
C++.
8.
String Functions (Contd.)
StringConcatenation:
String concatenation can be done by using the append()
function or the ‘+’ operator.
Syntax: str1.append(str2); (or) str3 = str1+str2;
Substring:
This function is used to take a substring from a given string.
If ‘m’ and ‘n’ are the beginning and end character indices, then
the substring displayed is from ‘m’ to ‘n-1’
Syntax: str.substr(beginning char index, ending char index);
str1.erase(start_index, end_index);
String Comparison:
The compare() function is used to lexicographically lcompare 2
strings. It returns a negative value if string 1 is less than string 2, 0 if
string 1 is equal to string 2 and a positive value if string 1 is greater
than string 2.
Syntax: str1.compare(str2);
Insertion, Replacement and Deletion Of Characters:
The replace() function is replace specified characters with a
string, while the insert() function is used to insert characters at the
specified location. The erase() function is used to delete a specified
set of characters.
Syntax: str1.insert(index, str2); str1.replace(start_index, str2,
end_index);
9.
String Functions (Contd.)
StringLength:
It is the simplest function which is used to print the
number of characters in a string.
Syntax: str.length(str);
Swapping of Strings:
The swap() function is used in swapping the contents of 2
string objects.
Syntax: str1.swap(str2);
Occurrence Of Character:
The find_first_of() is used to find the first occurrence of a
character in another string, whereas strrchr() is used to find the last
occurrence of a character.
Syntax: str.find_first_of(‘character’); (or) str.find_last_of(‘character’);
Accessing And Finding characters:
The at() function is used to accessing individual characters
and the find() function is used to find a given substring.
Syntax: str.at(character_index); str.find(“substring”);
Other than these functions, operators such as ==, =, !=, >, <, <= and >= are used to compare and assign strings. Other string functions
present in the class String include resize(), capacity(), max_size(), empty(), begin(), end(), rbegin() and rend().
10.
Summary
• Manipulation anduse of C-style strings become complex and inefficient.
• C++ provides a new class called string to overcome the deficiencies of C-strings.
• The string class supports many constructors, member functions and operators
for creating and implementing string objects.