Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Common String Functions in C++:
2.1.
String length example in C++
2.2.
String Copy Example in C++
2.3.
String Concat Example in C++
2.4.
String Compare Example in C++
3.
C++ String Functions
4.
 
5.
 
6.
 
7.
 
8.
 
9.
 
10.
 
11.
 
12.
 
13.
 
14.
 
15.
 
16.
 
17.
Frequently asked questions
18.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

C++ Strings

Author Akash
0 upvote

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. 

Fibonacci Series in C++

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;
}
You can also try this code with Online C++ Compiler
Run Code

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;
}
You can also try this code with Online C++ Compiler
Run Code

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;
}
You can also try this code with Online C++ Compiler
Run Code

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;
}
You can also try this code with Online C++ Compiler
Run Code

Output

0

In the above code, the res is 0. This means that both the strings are the same.

C++ String Functions

Here is a list of the string functions with their use.

String Functions

Description

getline() This function is used to read a string or a line from an input stream.
push_back(char ch) This function inserts a new character ch at the end of the string.
pop_back() This function removes the last character of the string.
size() This function returns the size of the string in terms of bytes.
length() This function is used to find the length of the string.
substr(int pos, int n) This function creates a new string object of n character.
c_str() This function returns a pointer to an array that contains a null-terminated sequence of characters.
empty() This function is used to check whether a string is empty or not.
clear() This function is used to remove all elements from the string.
resize(int n) This function is used to resize the length of a string.
capacity() This function returns the allocated space of the string.
begin() This function returns the reference of the first character.
end() This function returns the reference of the last character.
max_size() This function returns the maximum length of the string.
at(int k) This function is used to access an individual character at specified position k.
swap(string& str) This function is used to swap the values of two strings.
shrink_to_fit() This function decreases the capacity of the string and makes it equal to the size of the string.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Also check out - Substr C++

Frequently asked questions

  1. What are the string functions in C++?
    Ans: String functions are nothing, just functions that are used to perform operations on a string.
     
  2. Which string function is used for the concatenation of two strings?
    Ans: strcat(str1, str2) function is used to concatenate two strings.
     
  3. Which header file is required for string functions in C++?
    Ans: <string.h> is the header file required for string functions in C++.

Key Takeaways

We learned about string functions in this blog. This blog also discussed some string functions with the help of code.

You can learn more about such programming concepts here.

Check out this article - C++ String Concatenation

Recommended problems -

 

This blog is over, but learning never stops, and there is a lot more to learn. Have a nice day and Happy Coding!  

Live masterclass