Do you think IIT Guwahati certified course can help you in your career?
No
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.
Pointers enable efficient string manipulation but require careful memory management.
Difference between String and Character Array in C++
Parameters
std::string
Character Array
Header File
<string>
None
Mutability
Dynamic size, can grow/shrink
Fixed size, predefined
Ease of Use
Easier with built-in methods
Requires manual operations
Memory Management
Handled automatically
Needs manual management
Functionality
Rich with built-in functions
Limited functions available
Performance
Slightly slower
Faster for fixed-size data
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++
C++
// 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; }
You can also try this code with Online C++ Compiler
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
C++
C++
// 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; }
You can also try this code with Online C++ Compiler
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++
C++
// 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; }
You can also try this code with Online C++ Compiler
Here is a simple example of the string comparison using strcmp() function.
Code
C++
C++
// 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; }
You can also try this code with Online C++ Compiler
String functions are nothing, just functions that are used to perform operations on a string.
Which string function is used for the concatenation of two strings?
strcat(str1, str2) function is used to concatenate two strings.
Which header file is required for string functions in C++?
<string.h> is the header file required for string functions in C++.
Conclusion
Strings in C++ offer flexibility and versatility, making them essential for handling textual data in programs. With options like the dynamic std::string class and static character arrays, developers can choose the approach that best suits their needs. Whether taking input, passing strings to functions, or working with pointers, C++ provides robust tools for string manipulation. Understanding the differences and use cases of std::string and character arrays ensures better performance and more maintainable code.
You can learn more about such programming concepts here.