C++ String

tutorial and example
tutorial and example

string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class.

C Style String

The C style string of C language is also supported in C++ programming language. C string is an array of char and terminated by a null character ‘\0’.

String Declaration

There are two different way to declare C style string in C++ programming:

    1. Through array of character.

2. Through pointers.

String Initialization

String initialization is done by passing character in character (char) array end with null (\0). String initialization is also done by passing value in string name within double inverted comma (” “);

Memory Representation of String

CPP String

C++ String Example 1

Output:

String function used in C as well as C++

Function Example Description
strlen() strlen(s1) Return length of string s1.
strcmp() strcmp(s1,s2) Compare string s1 and
strcpy() strcpy(s1,s2) Copy string s2 into string s1.
strcat() strcat(s1,s2) Concatenate string s2 at the end of string s1.
strchr() strchr(s1,ch) Search character ch in string s1.
strstr() strstr(s1,s2) Search string s2 in string s1.

C++ String Example 2

Output:

C++ Standard Library String

C++ provides a string class to overcome the complexity and inefficiency of using C style string in C++ programming. C++ uses standard library <string> to handle all manipulation done over string object.

C++ String Example 3

Output:

In the above program <string> is the standard library that handle string manipulation and string in main() function is string class.

In C++, cin and >> operator read only one word of a string, compiler treats space as termination of string provided in string as value.

C++ string class supports many member functions, constructors and operators for manipulating string objects.

C++ String functions

Function Description
size() Return the number of characters in the string
length() Return the number of elements in a string
capacity() Gives the total elements that can be stored
max_size() Gives the maximum possible size of string of string object
insert() Insert character at a specific location
replace() Replace specific character within a given string
erase() Removes characters as specified
append() Append a part of string to another string
copy() Copy sequence of character from string
compare() Compare string against the invoking string
swap() Swap value of two string
getline() Read a line of string

C++ string also uses cin.get(ch) member function of istream class defined in <iostream> the member function cin.get(ch) reads the next character, even if it is a space, from the input and assigns it to the variable ch.

String Example 4

This program illustrate the use of size(), length()capacity()max_size() of string function.

Output:

String Example 5

This program illustrate the use of insert()replace()erase() of string function.

Output:

String Example 6

This program illustrate the use of cin.get() of string function.

Output:

In the above program parameters passed in cin.get(ch,10) function represents variable name and size of variable respectively.

String Example 7

This program illustrate the use of getline() of string function.

Output:

getline() function take input stream cin in first parameter and string object (str) in second parameter.

In C++ it is prefer to use getline() function instead of using cin>> and cin.get().