C++ Strings
Strings
Strings are used for storing text.
A string variable contains a
collection of characters surrounded
by double quotes.
C++ provides following two types of string
representations −
• The C-style character string.
• The string class type introduced with Standard
C++.
The C-Style Character String
Array of characters
terminated by NULL
character ‘\0’.
The null character
indicates the end of
the string.
Strings are always
enclosed by double
quotes. Whereas,
character is enclosed
by single quotes.
Character vs. String
A string constant is a sequence of
characters enclosed in double quotes.
For example, the character string:
char s1[2]="a"; //Takes two bytes of storage.
s1: a \0
On the other hand, the character, in single quotes:
char s2= `a`; //Takes only one byte of storage.
s2: a
Character vs. String
The C-Style Character String:
Declaring and Initializing Strings
Actually, you do not place the null character
at the end of a string constant. The C++
compiler automatically places the '\0' at the
end of the string when it initializes the array.
Example 1
Example 2
The String Class in C++
Example 3
Example 4: using the get
function
In
order to read an entire line of
characters including the spaces, C+
+ uses the “get’ function.
Example 5: using the getline
function
Example 6
Basic Operations
Counting the number of characters
in a string
Comparing strings
Copying a string
Combining strings
Searching within a string
Example 7
Counting the number of characters in a
string : Using strlen()
It is defined in <cstring> header file.
Counting the number of characters in
a string: using the length function
The length method returns the
number of characters in a string,
including spaces and punctuation.
Like many of the string operations,
length is a member function, and we
invoke member functions using dot
notation.
str.length().
Example 8
Counting the number of characters in a
string: using the length function
Comparing two strings
Stringsin C++ can be compared
using either of the following
techniques:
• String strcmp() function
• In-built compare() function
• C++ Relational Operators ( ‘==’ , ‘!
=’ )
Comparing two strings:
using strcmp()
It is defined in <cstring> header file.
Example 9
Comparing two strings:
using compare()
Thecompare() function compares
two strings and returns the following
values according to the matching
cases:
Example 10
Example 11: Comparing two strings:
using relational operators
Copying a string
using = assignment operator
Using strcpy() function
Example 12
Copying a string using the
assignment operator
Example 13
Copying a string using strcpy()
Concatenate two strings
The + operator can be used between
strings to add them together to
make a new string.
Using strcat()
Using append
Example 14: Using +
operator
Example 15: Using
strcat()
Example 16: using append
function
Searching within a
string
Thestring member function find is
used to search within a string for a
particular string or character.
Example 17: using find
function
References
https://www.journaldev.com/
https://web.stanford.edu/class/archiv
e/cs/cs106b/cs106b.1132/handouts/0
8-C++-Strings.pdf
https://www.tutorialspoint.com/cplus
plus/cpp_strings.htm
https://www.w3schools.com/cpp/
cpp_strings.asp