0% found this document useful (0 votes)
31 views5 pages

Strings in C++

The document discusses two ways to store strings in C++: using character arrays and using the string class. It provides details on initializing and manipulating strings using each approach, and compares their differences in terms of memory usage, speed, and available functions.

Uploaded by

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

Strings in C++

The document discusses two ways to store strings in C++: using character arrays and using the string class. It provides details on initializing and manipulating strings using each approach, and compares their differences in terms of memory usage, speed, and available functions.

Uploaded by

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

Strings in C++

Introduction:
Strings in C++ are used to store text or sequence of characters. In C++
strings can be stored in one of the two following ways:
• C style string (using characters)
• String class

Each of the above methods is discussed below:


1. C style string: In C, strings are defined as an array of characters.
The difference between a character array and a string is the string
is terminated with a special character ‘\0’. In C, the string is actually
represented as an array of characters terminated by a null string.
Therefore the size of the character array is always one more than
that of the number of characters in the actual string. This thing
continues to be supported in C++ too. The C++ compiler
automatically sets “\0” at the end of the string, during initialization of
the array.
Initializing a String in C++:
1. char str[] = "Geeks";
2. char str[6] = "Geeks";
3. char str[] = {'G', 'e', 'e', 'k', 's', '\0'};
4. char str[6] = {'G', 'e', 'e', 'k', 's', '\0'};
Below is the memory representation of a string “Geeks” in C++.

2. Standard String representation and String Class: In C++, one can


directly store the collection of characters or text in a string variable,
surrounded by double-quotes. C++ provides string class, which
supports various operations like copying strings, concatenating strings
etc.
Initializing string in C++:
1. string str1 = "Geeks";
2. string str2 = "Welcome to GeeksforGeeks!";

string class in C++


C++ has in its definition a way to represent a sequence of characters as an
object of the class. This class is called std:: string. The string class stores
the characters as a sequence of bytes with the functionality of
allowing access to the single-byte character.

String vs Character Array

String Char Array

A string is a class that defines A character array is simply an array of


objects that be represented as a stream characters that can be terminated by a
of characters. null character.

In the case of strings, memory is allocated The size of the character array has to
dynamically. More memory can be be allocated statically, more memory
allocated at run time on demand. As no cannot be allocated at run time if
memory is preallocated, no memory is required. Unused allocated memory is
wasted. also wasted
String Char Array

As strings are represented as objects, no There is a threat of array decay in the


array decay occurs. case of the character array.

Strings are slower when compared to Implementation of character array is


implementation than character array. faster than std:: string.

String class defines a number of Character arrays do not


functionalities that allow manifold offer many inbuilt functions to
operations on strings. manipulate strings.

Operations on Strings

1) Input Functions

Function Definition

This function is used to store a stream of characters as entered by the


getline() user in the object memory.

push_back() This function is used to input a character at the end of the string.

Introduced from C++11(for strings), this function is used to delete the


pop_back() last character from the string.

2) Capacity Functions
Function Definition

This function returns the capacity allocated to the string, which can
capacity()
be equal to or more than the size of the string. Additional space is
Function Definition

allocated so that when the new characters are added to the string,
the operations can be done efficiently.

This function changes the size of the string, the size can be
resize() increased or decreased.

length() This function finds the length of the string.

This function decreases the capacity of the string and makes it equal
to the minimum capacity of the string. This operation is useful to
save additional memory if we are sure that no further addition of
shrink_to_fit() characters has to be made.

3) Iterator Functions

Function Definition

begin() This function returns an iterator to the beginning of the string.

end() This function returns an iterator to the next to the end of the string.

rbegin() This function returns a reverse iterator pointing at the end of the string.

This function returns a reverse iterator pointing to the previous of


rend() beginning of the string.

This function returns a constant iterator pointing to the beginning of the


cbegin() string, it cannot be used to modify the contents it points-to.
Function Definition

This function returns a constant iterator pointing to the next of end of the
cend() string, it cannot be used to modify the contents it points-to.

This function returns a constant reverse iterator pointing to the end of


crbegin() the string, it cannot be used to modify the contents it points-to.

This function returns a constant reverse iterator pointing to the previous


of beginning of the string, it cannot be used to modify the contents it
crend() points-to.

4) Manipulating Functions:
Function Definition

This function copies the substring in the target character array


copy(“char mentioned in its arguments. It takes 3 arguments, target char
array”, len, array, length to be copied, and starting position in the string to
pos) start copying.

swap() This function swaps one string with another

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy