Category Technology
Date
string functions in c++ Learn about the functions of strings in C++ and take your coding skills to the next level.

To define a string in C++, its datatype refers to a character array in C++ language. It is crucial for help with C++ homework.  Furthermore, string functions refer to string operations used for the manipulation of string. These functions are part of the C++ string class and are essential tools in text processing, data input-output, user interaction, and debugging. 

Understanding string functions is vital for clean and efficient C++ coding. It can help with C++ homework and provide a skillset to work with text data for your project or a company. Therefore, let’s start by understanding string functions in C++.

Introduction to C++ Strings and Character Arrays

C++ is a popular programming language in the development circle. In fact, it is often considered one of the top programming languages for AI. Therefore, understanding the concepts of string in C++ will add more to the skillset.

In C++, there are primarily two ways to work with text data. These are character arrays and string functions. Ideally, character arrays and string functions both serve different purposes. However, string functions rely on character arrays for internal storage, null terminator, and underlying implementation.

Both of these have their usage and limitations. Therefore, let’s understand them both:

C-Style Character String - Character Array

C-style character strings are unidimensional arrays of characters terminated with a null character. Here are some important attributes related to them:

  • Basic Building Block: A character array is a fundamental data structure in C++. In essence, it is an array of individual characters.
  • Null Terminator:  It is an important aspect of character arrays. Null Terminator is a special character that signifies the end of the string within an array. It keeps track of string length for character arrays.
  • String Manipulation: C++ doesn’t have built-in functions for manipulating character arrays, such as searching or concatenation. The user must use library functions, such as strcpy (copy string) and strcat (concatenate strings), or write their loop functions.

Standard C++ String Class

Standard C++ string class (std::string) is part of the Standard Template Library (STL). These are some of the essential attributes to understand: 

  • Object-Oriented Approach: The C++ string class (std::string) is a modern and safer way to handle text data. It provides a string object with built-in functionalities. 
  • Automatic Memory Management: Unlike character arrays, the std::string class manages memory allocation. Also, it automatically handles the null terminator. The user doesn’t need to worry about array size limitations, as the string object can grow or shrink dynamically as required.
  • Rich Functionality: The std::string class offers many built-in methods to manipulate strings. These methods include functions for finding characters. For example, extracting substrings, concatenating strings, comparing strings, and modifying string content.

Key Difference Between C++ Strings and Character Arrays

Before taking a deep dive into string functions in C++ with examples, it is important to understand the difference between the two ways we can work with string. Therefore, Here's a table summarizing the key differences:,

Feature Character Array C++ String (std::string)
Type Fundamental data type Object
Memory Mgmt Manual Automatic
Null Terminator Required (programmer) Automatic
Size Limit Fixed during declaration Dynamically adjusted
Manipulation Requires library functions or custom loops Built-in member functions

mobile app development languages

How to Declare String in C++?

Declaring String in C++ is one of the most crucial things to learn. Without it, the user won’t be able to use the String functions. Therefore, these are the two main ways of declaring string:

1. Using the std::string class:

c++ strings

Figure 1: string definition using the standard C++ string class

It is the preferred method for a safer and much more convenient way to handle strings. It's part of the C++ Standard Library and requires including the header file.

Here's the syntax for declaring a string variable using std::string:

std::string str_1 = "My String";

Or it can be written as shown in figure 1:

String str_1 = “My String”;

This code declares a variable named str_1 of type std::string and initializes it with the value "My String".

2. Using C-style character arrays:

string in c++

Figure 2: C-style string declaration in C++

This is the older approach inherited from C. It involves declaring a character array and manually null-terminating it. C-style strings are less safe and require more manual memory management.

Here's how to make a string in C++ using a C-style string, as described in Figure 2:

char str_1[10] = {‘M’, ‘y’,‘ ’, ‘S’, ‘t’, ‘r’, ‘i’, ‘n’,‘’g,‘\o’};

Or

char str_2[] = "My String";

This code declares a character array named str_1 of size 10 (to accommodate the null terminator) and initializes it with the string "My String”.

Here's a table summarizing the key differences between the two approaches:

Feature std::string C-style String
Safety Safer Less safe (manual memory management)
Convenience More convenient Less convenient (manual null termination)
Memory Management Automatic Manual
Functionality Rich set of methods for string manipulation Limited functionality

In most cases, it's recommended to use std::string for string handling in C++ due to its safety and ease of use.

Essential Built-In Functions for C++ String Manipulation

The and headers in C++ provide a comprehensive set of built-in functions for string manipulation. However, to use std::string, the user needs to declare in the code's header of C++. 

Some essential functions of C++ string class (std::string) and cstring include:

size() Returns the length of the string
at() Access the character at a specific index
append() Adds a string to the end of another
insert() Insert characters in the string
substr() Extract a substring from another string
find() Returns the position of a substring’s first occurrence
swap() Swaps one string with the other
strcat() Combines two strings to form one
strcpy() Copies one string to other
strcmp() Compares two strings
resize() Changes the length of the string
capacity() Returns the maximum allowable size of the string
erase() Erase a particular part of a string

It is important to grab a hold of this C++ string functions list. Therefore, below, we have tried to explain them with more nuance and the syntax to use them:

size()

size() is amongst the only string functions in C++ that calculate string size. It is not a single, universally defined function. Instead, two main contexts surround it. These are:

1. Member function of the std::string class: 

This size() function is a member function of the std::string class (from the header). It returns the number of characters in the string object. The function excludes the null terminator.

Syntax:

std::string MyString = "Hello world!";
int StringLength = MyString.size();  // StringLength will be 12

2. Function from the header: 

The C++ Standard Library ( header) offers a generic size() function. This can be used with various containers and iterators. This size() function takes a range of iterators as input and returns the number of elements within that range.

Syntax:

#include 
#include 

std::vector MyVec = {1, 2, 3, 4, 5};

int VecSize = std::size(MyVec);  // vecSize will be 5

at()

The at(pos) function is used to access a specific character. It scans the character within a string object based on a specified index.

Syntax:

std::string::reference at(size_t pos);

Parameters:

pos: This is a mandatory parameter of type size_t (an unsigned integer type). It specifies the index of the character the user wants to access within a string. An unsigned integer must be used because it ensures that the index value passed is non-negative. 

Return Value:

The function returns a reference to the character at the specified position (pos). This specified position is within the string object. It allows the user to modify the character by using a returned reference.

Key Points:

  • Bounds Checking: at(pos) performs bounds checking. If the user accesses a character outside of a valid range, the system will show an out_of_range exception. This will help prevent potential crashes due to accessing non-existent characters. 
  • Modification: Since at(pos) returns a reference, it can directly modify the character at the specified position. For example, str.at(2) = 'x'; would change the third character (index 2) of the string object str to 'x'.

append()

append()function is used to append (attach) characters or another string object to the end of an existing string object.

Syntax:

std::string& std::string::append(const std::string& str);

Explanation:

  • std::string&: The return type is a reference to the modified std::string object (allowing for method chaining).
  • std::string::: This indicates it's a member function of the std::string class.
  • append: Name of the function.
  • const std::string& str: This parameter represents the string to be appended. It's passed by reference to avoid unnecessary copying.

There are two main ways to use the append() function. Here are the syntax:

1. Appending a String Object:

std::string STR1 = "Hello";
std::string STR2 = " World!";

STR1.append(STR2); // Append str2 to the end of str1

In this case, the values from the characters STR2 are appended to STR1. This will result in a modified value of “Hello World”.

2. Appending a Character Array (C-Style String):

std::string STR = "Welcome to ";
char CSTR[] = "C++ programming";

STR.append(CSTR); // Append the C-style string (CSTR) to STR

insert()

The insert() function in C++ is used with containers, such as vectors and strings, to insert elements at a specific position. The function's variations depend on the container and type of insertion. 

Below is the breakdown of two common containers:

1. insert() function for vectors:

The insert() function for vectors (from the header) inserts elements at a specified position within the vector. Here's the syntax:

iterator_name insert(iterator_position, const_element_type& element);

Parameters:

  • iterator_position: An iterator pointing to the position where you want to insert the new element.
  • const_element_type& element: The element (value) to be inserted.

2. insert() function for strings:

The insert() function for strings (from the header) offers multiple ways to insert elements (characters or substrings) into a string object. The general syntax used is:

iterator_name insert(iterator_position, const_string_type& str);
iterator_name insert(iterator_position, size_type count, char value);

Variations:

  • Inserting a substring: Pass an iterator pointing to the insertion position and a string object (str) containing the characters to insert.
  • Inserting multiple characters: Pass an iterator, the number of characters (count) to insert, and the character value to be inserted repeatedly.

substr()

In C++, the substr() function extracts a substring (a portion of the string) from a given string object.

Syntax:

std::string::substr(size_t pos, size_t len = npos) const;

Parameters:

  • pos: The parameter specifies the starting position (index) within an original string. It is where the substring extraction begins. Also, remember the first character of the string starts with index 0.
  • len (optional): This parameter specifies the length of the substring to be extracted. The variable represents the number of characters to include in the substring that starts from the pos position.
  • If len is omitted or set to std::string::npos (value representing "not found"), the function extracts the substring from the pos position to the end of the original string.

Return Value:

Once executed, the substr() function returns a new std::string object that holds the extracted substring. Any modifications to the returned substring will not affect the original string object.

Key Points:

  • Bounds Checking: The substr() function performs bound checking implicitly. The function returns an empty string if the pos is greater than or equal to the original string.
  • No Modification to Original String: The substr() function only extracts a portion of the string and returns a new string object. However, it doesn't modify the original string object itself. 

find()

There are primarily two common meanings of “find” in C++. Here’s a breakdown for both of them:

1. std::find in header:

The function is used to find the first occurrence of a specific value. This value is within a range of elements stored in a sequence. The generic “find” function is capable of working with various iterators. It makes it a versatile solution to deal with different data structures. 

Syntax:

#include 

iterator std::find(iterator first, iterator last, const T& value);

Parameters:

  • first: An iterator pointing to the beginning of the search range.
  • last: An iterator pointing to the element just after the end of the search range (not the last element itself).
  • value: Value the user is searching for.

Return Value:

  • Return picks the first value in the occurrence within the specified range.
  • If no value is found, the iterator reaches the end. After that, it returns the value of the last iterator.

2. std::string::find in header:

This function is designed for searching within C++ strings (objects of the std::string class). It helps you find the first occurrence of a substring or character within the string.

Syntax:

#include 

size_t std::string::find(const std::string& str, size_t pos = 0); // Find substring
size_t std::string::find(const char* s, size_t pos = 0);        // Find character (C-style string)

Parameters:

For substring search:

  • str: The substring you're searching for.
  • pos (optional): The starting position (index) within the string to begin the search (defaults to 0).

For character search:

  • s: A null-terminated character array representing the character you're searching for (C-style string).
  • pos (optional): Similar to the substring search.

Return Value:

  • If found, the index (position) of the first occurrence of the substring/character within the string.
  • If not found, it returns a special value (std::string::npos) indicating the unsuccessful search.

swap()

The swap() string function is used to swap the values of two strings. However, there are two main approaches associated with it:

1. Using the std::swap function:

This is the recommended approach with modern C++ development. The std::swap function is built-in from the header. It efficiently swaps the values of two variables.

Syntax:

void std::swap(T& a, T& b);

Explanation:

  • T: It is a placeholder for the data type of the variables you wish to swap. It can be any data type. It includes built-in types like int, double, or user-defined types like classes or structs.
  • a and b: These are references to the variables whose values you want to swap. Using the references, the function modifies the original variables directly, avoiding unnecessary copying.

2. Using a temporary variable:

It is a more traditional approach. However, it can be less efficient and readable compared to std::swap.

Syntax:

T temp = a;
a = b;
b = temp;

Explanation:

  • T: It is the data type of the variables you want to swap.
  • temp: It is a temporary variable of the same type as a and b. It is used to hold the value of one variable while the other variable's value is overwritten.

In summary:

  • Use std::swap for modern C++ development. It is efficient, safe, and avoids unnecessary copying.
  • The temporary variable method can still be used in specific situations. However, it is generally less preferred.

strcat()

The strcat function is used to concatenate two strings. The function is technically available through C++ via header. However, it is not generally recommended due to potential issues. Here’s a breakdown:

Syntax:

char *strcat(char *dest, const char *src);

Parameters:

  • dest: Pointer to the destination character array where the source string will be appended.
  • src: Pointer to the source null-terminated character array (string) to be appended.

Return Value:

  • The function returns a pointer to the destination string (dest).

strcmp()

The strcmp is a C++ compare string function. It compares two strings lexicographically (character by character). The function is defined in the header library.

Syntax:

int strcmp(const char* str1, const char* str2);

Parameters:

  • str1: (const char*) A pointer to the first string (null-terminated character array) to be compared.
  • str2: (const char*) A pointer to the second string (null-terminated character array) to be compared.

Return Value:

strcmp returns an integer value based on the comparison result:

  • 0: The two strings are equal.
  • positive value: The first string (str1) is lexicographically greater than the second string (str2). This happens when the first difference between corresponding characters in str1 and str2 is that the character in str1 has a higher ASCII value than the character in str2.  
  • negative value: The first string (str1) is lexicographically less than the second string (str2). This happens when the first difference between corresponding characters in str1 and str2 is that the character in str1 has a lower ASCII value than the character in str2.

resize()

The resize() function modifies the size of a string object by increasing or decreasing its capacity to hold characters. There are two main ways to use the resize() function:

1. Resize to a new size without fill character:

std::string MyString = "Hello";
MyString.resize(10);  // Resize to hold 10 characters (including null terminator)

In this case, the string "Hello" will be resized to hold 10 characters. The existing characters ("Hello") will be preserved, and the remaining characters (up to 3 in this case) will be filled with null terminators (\0).

2. Resize to a new size with a fill character:

std::string MyString = "World";
MyString.resize(15, '*');  // Resize to hold 15 characters, filling with '*'

The string "World" will be resized to hold 15 characters here. The existing characters ("World") will be preserved, while the remaining 12 characters will be filled with asterisk characters (*).

Key Points:

  • Increasing Size: If the new_size is larger than the current size, the string object will grow to accommodate the new size. It will fill the additional space with the specified character (or null terminators by default).
  • Decreasing Size: If the new_size is smaller than the current size, the string object will be truncated to the new size. Any characters beyond that new size will also be removed.
  • No Content Modification: The resize() function doesn't modify the existing characters within the string. Contrarily, it adjusts the capacity of the string object to hold more or fewer characters.

capacity()

The capacity() function is a member function associated with the std::string class in C++. It determines the storage capacity of a std::string object. This refers to the maximum number of characters it currently holds without reallocating memory.

Here's a breakdown of the capacity() function:

size_type capacity() const;

  • size_type is a typedef representing an unsigned integer type suitable for storing string lengths and positions (typically an unsigned long).
  • const: It indicates that the function doesn't modify the std::string object itself (a const member function).

Syntax:

std::string MyString = "This is a string";
size_t capacity = MyString.capacity();

This code snippet first creates a std::string object named MyString. It initializes it with the value "This is a string". After that, it calls the capacity() function on the MyString object and stores the returned value (the storage capacity) in the capacity variable.

Return Value:

The capacity() function returns a size_t value. It represents the number of characters the current std::string object can hold before allocating more memory. This value is not necessarily equal to the current length of the string (accessible using size() or length() functions).

Key Points:

  • Dynamic Memory Allocation: std::string objects manage their memory allocation internally. When you create a std::string object, it allocates some initial memory to store characters. The capacity() function reflects the size of this allocated memory block.
  • Growth and Shrinking: As you add characters to the string using functions like append or the + operator, the std::string object might automatically reallocate memory. This is the case if the current capacity is reached. Similarly, if you remove characters and the string shrinks significantly, the std::string object might reduce its allocated memory to optimize space usage.
  • Not the Current Length: The capacity() function tells you about the maximum potential size. However not the current number of characters in the string. The user can use the size() or length() functions to determine the current length (number of characters excluding the null terminator).

erase()

The erase() function in C++ removes specific elements or a range of elements from a string object. There are two main ways to use the erase() function:

Syntax:

1. Erase by position:

std::string::iterator erase(iterator position);

  • position: An iterator pointing to the element you want to remove. The iterator returned by the function points to the element that now occupies the position. This is after the erased element. If you erase the last element, the function will return the end iterator of the string.

2. Erase by range:

std::string::iterator erase(iterator start, iterator end);

  • start: An iterator pointing to the first element you want to remove (inclusive).
  • end: An iterator pointing to the element after the last element you want to remove (exclusive). The function returns the iterator pointing to the element now occupying the position after the erased elements.

Here's a breakdown of the parameters:

  • iterator: In C++, iterators are objects that point to elements in a container (like a string). The user can use iterators to traverse elements and access their values. This is considering that std::string provides iterators to access characters within the string object.

Return Value:

The erase function returns an iterator pointing to the element now occupying the position after the erased element(s). If you erase the last element, it returns the string's end iterator.

cross platform app development companies

String Functions in C++ with Examples + Manipulations Techniques!

Learning how string works and manipulating it is one of the key tips to become an app developer. Whatever the case may be, without examples, it is often hard to understand a concept. With our examples below, we have tried to give practical examples of reinforced learning:

string functions in c++

Figure 3: Practical examples of string functions in C++

cout<<size(str_1)<<endl; - Row 12

Returns the length of the string str_1. size() is a string capacity function.
str_3.insert(6,”Hi”); - Row 13 Insert string “Hi” at index # 6 in str_3
swap(str_1, str_2); - Row 15 Swaps C-style character string str_1 with str_2
strcat(str_1, str_2); - Row 18 Combines C-style character string str_1 with str_2 and

c++ strings

Figure 4: Practical examples of string functions in C++

strcpy(str_1, str_2); - Row - 12 Copies C-style character string str_2 to str_1

cout<strcmp(str_1,str_2)<<endl; - Row 14

Compares strings str_1 with str_2 and returns 0, i.e., strings don’t match

str_3.resize(30); - Row 15

Changes the length of string str_3 to 30. resize() is a string capacity function.

cout<<str_3.capacity()<<endl; - Row 17

Returns the maximum allowable size of string str_3.capacity() is a string capacity function.

Learning about how a string function works with C++ code is one thing; however, we must learn string manipulation techniques to grasp its concepts fully. It will broaden our horizons and help us achieve the desired coding results.

Utilizing C++ String Input Functions

String input functions in C++ read input text, i.e., character arrays and strings from the user. The character input function cin() accepts a stream of characters until it receives the terminating character (space, enter, tab). 

The second string input function is gets() that accepts the character array until the user presses ‘Enter’. It is suitable for capturing a complete sentence. Another string input function getline() is the part of header. 

getline() is especially useful when reading long strings or character arrays that include spaces. String input function getline() is generally recommended over gets() due to buffer overflow risk in the latter. Moreover, getline() is a non-member overload of the C++ string class.

Exploring C++ String Iterator and Capacity Functions

Using iterators is a flexible and common way to iterate through the characters of a string in a forward or backward direction. They are members of the C++ string class and are widely used in string manipulation tasks. 

Let’s illustrate string iterators using the simple string operation of traversing each character.

string in c++

Figure 5: Iterating string using string iterators in C++

A simpler way of doing the same task is to iterate each character by indexing the characters in the string using a loop. The use of the auto() keyword is another string manipulation approach for iterating through the string. Example code explains its use.

string functions in c++

Figure 6: Use of auto() keyword for string iteration

String capacity functions ease optimization and resource management as they control the memory management of strings. 

Figures 3 and 4 show examples of some string capacity functions in the C++ string class. Another useful string capacity function is shrink_to_fit(), which makes the string size equal to the length of the string contained by it to save memory.

Other Advanced String Manipulation Techniques:

  1. String Hashing uses hash functions to create hash values for strings.
  2. OpenMP for concurrent string manipulation.
  3. Converting strings to other data types using std::stringstream
  4. For efficient substring extraction, use std::string_view()
  5. Unicode characters handling and supporting internationalization ICU (International Components for Unicode) library.

Conclusion

Understanding string functions in C++ is an important skill. If you aim to excel as a developer in any of the top custom software development companies, understanding string is an indispensable skill. It also enables developers to build logic efficiently to handle textual data. It is an integral part of every software or app development cycle. It is a collateral skill for which we tried to offer an impactful resource through this article.

 

Frequently Asked Questions

  • What is a string in programming?

    Image Image

    In programming, a string is a fundamental data type used to represent text. In essence, it is a sequence of characters that can be letters, numbers, symbols, or even spaces.

  • What are the basics of string in C++?

    Image Image
  • What are the different C++ compare string functions?

    Image Image
  • What header file is needed for string functions?

    Image Image
Sakshi Kaushik

By Sakshi Kaushik LinkedIn Icon

A passionate writer and tech lover, she strives to share her expertise with mobile app developers and fellow tech enthusiasts. During her moments away from the keyboard, she relishes delving into thriller narratives, immersing herself in diverse realms.

Uncover executable insights, extensive research, and expert opinions in one place.