C++ String Functions
Today we are going to talk about C++ string functions. In any programming language string is core part to learn. All programming languages have nearly the same concept related to string. This feature gives us the ability to articulate our code.
What is the string?
A string is a sequence of characters treated as a single object in c++. We have a string class in C++.
This string class is used to initialize the objects, which holds a sequence of characters.
To use string functions in C++ we need to add 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
4.5 (449 ratings)
String Greeting = “Hello World!”;
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:
String greeting ;
Cout<< ” The common sentence in programming is: ”<< greeting << endl;
Here our goal is to learn how can we play with the string.
//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;
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.
string s = “C++ is an easy language”;
Index starts at 0
string language = s.substr(0,3); // output of substr storing in language variable.
cout << language << endl;
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.
String str1 = "I love my";
string str2 = " Country";
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|
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.
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.
string str1 = "c++ is object oriented language";
int length = strlen(str1);
cout << "Length of str1 is : " << length << endl;
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
cout<<”What is your name: ”<<endl;
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.
- Balgurusami (Object-Oriented Programming with C++)
- Object-Oriented Programming in C++ by Robert Lafore (Publisher: Pearson)
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–