C++ Manipulating Strings
A string is a sequence of character. As you know that C++ do not support built in string type, you have use earlier those null character based terminated array of characters to store and manipulate strings. These strings are termed as C Strings. It often become inefficient performing operations on C strings. Programmers can also define their own string classes with appropriate member functions to manipulate strings. ANSI standard C++ introduces a new class called string which is an improvised version of C strings in several ways. In many cases, the strings object may be treated like any other built in data type. String is treated as another container class for C++.
The C Style String
The C style string belongs to C language and continues to support in C++ also strings in C are one dimensional array of characters which gets terminated by \0 (null character).
This is how the strings in C are declared:
char ch[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
Actually, you do not place the null character at the end of a string constant. The C++ compiler automatically places the \0 at the end of the string when it initializes the array.
String Class in C++
The string class is very huge and includes many constructors, member functions and operators.
Programmers may use the constructors, operators and member functions to achieve the following:
- Creating string objects
- Reading string objects from keyboard
- Displaying string objects to the screen
- Finding a substring from a string
- Modifying string
- Adding objects of string
- Comparing strings
- Accessing characters of an string
- Obtaining the size or length of string, etc…
Manipulate Null-terminated strings
C++ supports a wide range of functions that manipulate null-terminated strings. These are:
- strcpy(str1, str2): Copies 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.
Important functions supported by String Class
- append(): This function append a part of string to another string
- assign():This function assigns a partial string
- at():This function obtains the character stored at a specified location
- begin():This function returns a reference to the start of the string
- capacity():This function gives the total element that can be stored
- compare():This function compares string against the invoking string
- empty(): This function returns true if the string is empty
- end():This function return a reference to the end of the string
- erase():This function remove character as specified
- find():This function searches for the occurrence of a specified sub string
- length(): It gives the size of 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
- String(): This constructor is used for creating an empty string
- String(const char *str): This constructor is used for creating string objects from a null terminated string
- String(const string *str): This constructor is used for creating a string object from another string object
Operators used for String Objects
- =: assignment
- +: concatenation
- ==: Equality
- !=: Inequality
- <: Less than
- <=: Less than or equal
- >: Greater than
- >=: Greater than or equal
- []: Subscription
- <<: Output
- >>: Input
No comments:
Post a Comment