String manipulation in C++: The manipulation of strings in C++ can be defined as we have already learned that C++ will not support the built-in string type. So, we have used the null character based terminated array of characters to store the manipulated strings.
String manipulation in C++
Manipulating Null terminated Strings
C++ supports a wide range of functions that manipulate null-terminated strings. They are as follows:
- strcpy(str1, str2): It copies the string str2 into string str1.
- strcat(str1, str2): Concatenates string str2 onto the end of string str1.
- strlen(str1): Returns the length of string str1.
- strcmp(str1, str2): Returns 0 if str1 and str2 are the same; less than 0 if str1<str2, greater than 0 if str1>str2.
- strchr(str1, ch): Returns a pointer to the first occurrence of character ch in string str1.
- strstr(str1, str2): Returns a pointer to the first occurrence of string str2 in string str1.
Functions Supported by string Class in C++
Function | Description |
---|---|
append( ) | appends a part of a string to another string |
assign( ) | The function assigns a partial string |
at( ) | function obtains the character stored at a specified location |
begin( ) | The function returns a reference to the start of the string |
capacity( ) | Gives the total element that can be stored |
compare( ) | compares a string against the invoking string |
empty( ) | returns true if the string is empty |
end( ) | returns a reference to the end of the string |
erase( ) | removes the character as specified |
find( ) | searches for the occurrence of a specified substring |
length( ) | It gives the size of a string or the number of elements of a string |
swap( ) | This function swaps the given string with the invoking one |
Important Constructors Obtained by String Class
Constructor | Description |
---|---|
String( ) | the constructor is used for creating an empty string |
String(const char *str) | used for creating string objects from a null-terminated string |
String(const string *str) | used for creating a string object from another string object |