Introduction
Before knowing about string functions, we have to know about string. In C++ string is an object of a string class representing the sequence of characters. String functions are nothing, just functions that are used to perform operations on a string. To use these string functions in C++, you need to add a library named <string> in your code at the top, which gives you various string functions like strlen, strcmp, strcat, swap, strcpy, and many more.
Common String Functions in C++:
- strlen(str1): It is used to find the length of a string.
- strcpy(str1,str2): It is used to copy the content of a string into another.
- strcat(str1,str2): It is used to append two strings.
- strcmp(str1,str2): this string function returns negative value if str1 is less than str2, 0 if str1 is equal to str2, and positive value if str1 is greater than str2.
-
substr(starting index, from that index how many characters you want): It is used to take the substring from the given string.
Now we will discuss how to use the string function in C++ with the help of some examples.
String length example in C++
Here is a simple example of finding the string length using strlen(),size() and length() functions.
Code
// C++ Program to demonstrate the working of strlen(), size() and length()
#include<iostream>
#include<string.h> // for string class
using namespace std;
int main()
{
// Initializing the string
char str1[]="codingninjas";
string str2="string functions are very useful";
cout<<"The length of the first string using strlen(): ";
cout<<strlen(str1)<<endl;
cout<<"The length of the second string using size(): ";
cout<<str2.size()<<endl;
cout<<"The length of the second string using length(): ";
cout<<str2.length()<<endl;
return 0;
}
Output
The length of the first string using strlen(): 12
The length of the second string using size(): 32
The length of the second string using length(): 32
String Copy Example in C++
Here is a simple example of copying the string using strcpy() function.
Code
// Simple C++ Program to demonstrate the working of strcpy()
#include<iostream>
#include<string.h> // for string class
using namespace std;
int main()
{
// Initializing the string
char str1[50]="Coding Ninjas";
char str2[50];
strcpy(str2,str1);
cout<<"The value of the first string is: ";
cout<<str1<<endl;
cout<<"The value of the second string is: ";
cout<<str2<<endl;
return 0;
}
Output
The value of the first string is: Coding Ninjas
The value of the second string is: Coding Ninjas
String Concat Example in C++
Here is a simple example of the string concatenation using Strcat() function
Code
// C++ Program to demonstrate the working of strcat()
#include<iostream>
#include<string.h> // for string class
using namespace std;
int main()
{
// Initializing the string
char str1[]="Coding";
char str2[]=" Ninjas";
strcat(str1,str2);
cout<<"The value of the first string is: ";
cout<<str1<<endl;
cout<<"The value of the second string is:";
cout<<str2<<endl;
return 0;
}
Output
The value of the first string is: Coding Ninjas
The value of the second string is: Ninjas
Try and compile with online c++ compiler.
String Compare Example in C++
Here is a simple example of the string comparison using strcmp() function.
Code
// C++ Program to demonstrate the working of strcmp()
#include<iostream>
#include<string.h> // for string class
using namespace std;
int main()
{
// Initializing the string
char str1[]="Coding";
char str2[]="Coding";
int res=strcmp(str1,str2);
cout<<res<<endl;
return 0;
}
Output
0
In the above code, the res is 0. This means that both the strings are the same.