Strings cs
Strings cs
Introduction
Strings in C++ are a sequence of characters, represented using the std::string class from the
Standard Template Library (STL). They are used to store and manipulate text or character data.
The std::string class provides various member functions and operators for string operations,
such as concatenation, length, comparison, and substring extraction. C++ also supports string
literals, which are sequences of characters enclosed in double-quotes.
Dynamic Size: Strings represented by a string in Fixed Size: Character arrays have a fixed size
C++ allow for dynamic resizing, meaning you can determined during declaration, and the size
easily add, remove, or modify characters within cannot be easily changed without manually
the string without worrying about managing the managing memory allocation and deallocation.
memory manually.
Manual Memory Management: Unlike strings,
String Manipulation: String class provides a rich character arrays require manual memory
set of functions for string manipulation, including management, meaning you need to allocate
concatenation (+ operator), finding substrings memory, to free the memory.
(find()), and extracting substrings (substr()),
among others. C-style String Functions: Character arrays
are compatible with traditional C-style string
Enhanced Functionality: The string class provides functions such as strcpy(), strlen(), strcmp(),
additional functionality like string comparison (==, which provide basic string manipulation
!=, <, >, etc.), and string length (length()). operations.
1
Initialization of Strings
Syntax:
Example:
To read a single string input from the user, you can use the cin stream extraction operator (>>).
Here's an example code snippet:
Syntax:
Example:
Input:
Output
getline
The getline function allows you to read a line of text including spaces until a newline character is
encountered. The getline function is used with cin as the input stream and string variable to
store the input. The user can enter a line of text, and the entire line is stored in the variable,
including spaces.
Syntax:
Example:
Input:
Output:
cin.ignore():
The cin.ignore() function discards or skips characters from the input stream. It is commonly
used after reading data using cin to ignore any remaining characters, such as newline
characters (‘\n’ - when you press enter), in the input stream. It takes the number of characters to
be ignored. Here's an example code snippet:
Syntax:
Example:
Input:
Output
Comparison Operators in Strings
Strings can be compared using various comparison operators, such as == (equal to), > (greater
than), < (less than), >= (greater than or equal to), and <= (less than or equal to). These
operators compare strings based on their lexicographical order, which means they are
compared character by character.
‘==’ operator
The == operator is used to check if two strings are equal. It returns true if the strings have the
same content and false otherwise. Here's an example code snippet:
Example:
Output:
The > operator is used to check if one string is lexicographically greater than another. It returns
true if the left operand is greater than the right operand and false.
Example:
In this code, the > operator compares str1 and str2. Since "Banana" comes after "Apple" in
lexicographical order, the output will be "str1 is not greater than str2."
Output
Example:
Output
Accessibility in Strings
Accessibility in strings refers to the ability to retrieve and modify individual elements or
characters within a string. This includes methods such as operator[] and at(), as mentioned in
the previous subtopics. These methods provide direct access to elements in a string, allowing
you to read or modify specific characters.
Operator[]
The operator[] is used to access individual characters in a string by their index. It provides direct
access to the characters and allows modification. The indexing starts at 0, where the first
character has an index of 0, the second character has an index of 1, and so on. Here's an
example code snippet:
Syntax:
Example:
Output:
at() function
The at() function provides a way to access individual characters in a string by their index,
similar to the operator[]. However, at() performs bounds checking and throws an out_of_range
exception if the index is out of range. Here's an example code snippet:
Syntax:
Example:
Output:
Traversal in Strings
Iteration allows you to traverse and process each character in a string sequentially. By
leveraging the accessibility features and methods available for strings, we can employ loops to
iterate over and perform manipulations on the elements of a string.
For loops
By using a normal for loop, you can iterate over a string using the index positions. The .size()
function helps determine the length of the string, allowing you to iterate from index 0 to size() -
1. Each character at the current index can be accessed using the str[i] syntax.
Output:
Alternatively, you can use a range-based for loop to iterate over each character in a string
directly. This loop simplifies the code by automatically handling the index and providing direct
access to each character without explicitly using the .size() function.
Example:
Output:
A range-based for loop is used to iterate over the characters in the string. The loop
automatically iterates over each character in the string, assigning it to the variable ch in each
iteration. The output displays each character one by one.
Operations on Strings
The push_back() function in C++ allows us to append a single character at the end of a string. It
takes a character as a parameter and adds it to the string's existing characters.
Example:
Output:
insert()
The insert() function in C++ enables us to insert characters or substrings at specific positions
within a string. It takes two parameters: the position at which insertion should occur and the
string or character to be inserted.
Example:
Output:
pop_back()
The pop_back() function in C++ removes the last character from a string. It doesn't return the
removed character but modifies the string directly.
Example:
Output:
erase()
The erase() function in C++ removes a portion of a string specified by its starting position and
length. It modifies the string directly and doesn't return anything.
Example:
Output:
'+' operator
In C++, the '+' operator can be used to concatenate two strings together. It creates a new string
containing the combined characters of both strings.
Example:
Output:
append()
The append() function in C++ allows us to concatenate one string to the end of another string. It
modifies the original string by adding the characters of the appended string.
Example:
Output:
Functions/Operators Description