C++ String Functions
String function are the functions that are used to perform operations on a string. C++ uses <string.h> library to provides various string functions like strcat, strlen, strcmp, strcpy, swap, and many more where strcat is used to concatenate string, strlen will calculate the length of the string, strcmp is used to compare two strings, strcpy will copy one value of the string to another, a swap is used to swap value between strings.
What is the string?
To use string functions in C++ we need to add a library named <string> in our code at the top, which gives you string functions. It must be included with the header file #include <string>. As we know there are many behaviors that string object understands and several operations we can perform on the string object.
Examples of String Functions in C++
Here we will discuss how to use string function in C++ programming with the help of examples
Example #1
String Greeting = "Hello World!";
Cout<<Greeting;
Which gives the following Output
Output: Hello World!
As we know cout<< is used to print on the screen in c++, and cin>> is to take input on the screen.
Let’s see the following example to be more precise:
Example #2
String greeting ;
Cin>>greeting;
Cout<< " The common sentence in programming is: "<< greeting << endl;
Here our goal is to learn how can we play with the string.
Example #3
//Suppose we have three string variables
string str1 = "ice"; //initialized with value
string str2 = "cream"; //initialized with value
string str3; // empty string
str3 = str1 + str2; // Here we are concatenating the string
cout << str3;
Output: icecream
How we achieved this?
The standard string class in c++ overloads the assignment operator (=). To be more clear see Example #3. We have three objects str1, str2, str3. We concatenated two strings i.e str1 and str2 and the value are get copied into str3. that means assignment operator got overloaded and new value in our case is icecream got copied into str3.
The string class has a default constructor that initializes string object to an empty string. Standard c++ has another constructor which takes the value(ex.str1 and str2 has the value assigned, and str3 is empty)
Following are some of the C++ String functions we can use:
- Substr(beginning char index, from that index how many characters you want.)
- Strcat(str1,str2): Appending the string
- Strcmp(str1,str2): Returns -ve value if str1 is less than str2;0 if str1 is equal to str2; and >0 (+ve value) if str1 is greater than str2.
- Strcpy(str1,str2): Replace the content
- Strlen(str1): Gives the length of the string
Substr() : This function is very simple one. As the name suggests it. take the substring from the given string. This function takes two parameters.
- The first parameter suggests starting index no.
- The second parameter suggests how many characters you want from the starting index.
Example #4
string s = “C++ is an easy language”;
c | + | + | i | s | a | e | a | s | y | l | a | n | g | u | a | g | e | ||||
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
Index starts at 0
string language = s.substr(0,3); // output of substr storing in language variable.
cout << language << endl;
Output: c++ Starting index is 0 and we need three characters from the 0th index so 3 is the second parameter. The second parameter works from 1 to n. not from 0 to n. so first three char gives us c++.
Strcat(): This string function in C++ combines two different strings, As shown in Example #5.
Example #5
String str1 = "I love my";
string str2 = " Country";
strcat(str1, str2);
cout << str1 ;
In the above example, the strcat function takes the copy str2 value and put it in str1. It combines to and gives output as below:
Output: I love my Country
Strcmp(): As the name suggests this compares two strings and gives back the result.
Suppose we have two strings str1 and str2.
Following table shows exact output for better understanding:
str1 < str2 | Returns –ve value |
str1 == str2 | Returns 0(Zero) |
str1 > str2 | Returns +ve value |
Example #6
string str1 = "We have seven Continents in the wolrd";
string str2 = "We have seven Continents in the wolrd";
int result = strcmp(str1,str2);
cout << result <<endl;
Output: 0 // both the strings are equal str1 == str2 In other cases, it returns ASCII value of string depending on the character at that position.
Strcpy(): unlike strcat, it will not append string into other . it will replace all the content.
Example #7
string str1 = "World is beautiful";
string str2 = "Yes we can";
string str3 = strcpy(str1,str2); // simply replace all the content in str1 with the content of str2
cout<< str3 <<endl;
Output: Yes we can
Strlen(): The simplest function in a row.
This function defined in <cstring> header file. This function returns the length of the string.
The length of a string is determined by the terminating null-character at the end \0.
Example #8
string str1 = "c++ is object oriented language";
int length = strlen(str1);
cout << "Length of str1 is : " << length << endl;
Output: 31
getline(): C++ string library functions also provide the getline function to read the whole line.
This function takes arguments as follows:
- It takes the first argument as a stream to read from.
- Second, it takes the input line
- And third, that stops the extraction
Example #9
cout<<"What is your name: "<<endl;
string str1;
getline(cin, str1 '\n');
cout<< "your name is : " << str1 << endl;
Output: What is your name: John
Your name is John
One of the most useful data types supplied in the C++ libraries is the string.
For a better understanding of string, you must code while learning. We can’t get the actual concept without writing the code.
BOOKS
- Balgurusami (Object-Oriented Programming with C++)
- Object-Oriented Programming in C++ by Robert Lafore (Publisher: Pearson)
Recommended Articles
This has been a guide to C++ String Functions. Here we discussed how to use string function in C++ programming with the help of examples. You can also go through our other suggested articles to learn more–