Some Useful Built-in String Functions in C++

In this tutorial, we are going to discuss some useful built-in string functions in C++ and their use.

Most Useful Built-in String Functions in C++

Here we will learn the following string functions in C++

  • strcat()
  • strncat()
  • strpbrk()
  • strcoll()
  • stoll()

Strcat() in C++

The strcat() function appends a copy of an string to another string.

Suppose, we want to append string 2 to string 1.

Syntax :-          strcat( s1, s2 );

It will append a copy of string 2 to string 1;

Code Snippet

 
 int main()
{
   string1 = "Codespeedy";
  
   string2 = " is the best site";

   cout<<strcat ( string1, string2 );
}

It will print the output as concatenation of string 1 and string 2 i.e. ” Codespeedy is the best site”.

Strncat() in C++

It concatenates a given number of bytes of element from one string to another. It accepts three arguments.

  1. String 1 in which ‘n’ characters from second string to be concatenated.
  2. String 2 from which ‘n’ character need to be concatenated in string 1.
  3.  number of characters from string 2 to be copied in string 1.

Syntax :-              strncat( string 1, string 2, n );  

Code Snippet

 int main()
{
   string1 = "Code";
   
   string2 = "speedysite";

   cout<<strncat( string1, string2, 6);
}

It will concatenate 6 characters from string 2 to string 1. So, output will be ” Codespeedy “.

Did You Know?

strncat is assumed to be safer in use than strcat because strcat does not check for the size of data to be copied which can cause buffer overflow while strncat checks for size of data to be copied and exactly copies ‘n’ bytes of data.

Strpbrk() in C++

It is used to find the first matching character in one string comparing to another string.

Syntax :-                       char* strpbrk ( const char* s1, const char* s2 );

s1 :-  string which needs to be scanned

s2 :- string from which character needs to be check.

It returns a pointer if character matches otherwise NULL.

Code Snippet

 int main()
{
  string s1= "Apple";
  
  string s2 = "Grapes";

   char *t;

   r = strpbrk ( s1, s2 );

     cout<<*r<<endl;
}

It will give the output ‘p’.

Strcoll() in C++

strcoll() function compares two strings and returns an integer value. It is defined in <string.h> header file.

Syntax :-              int result = strcoll ( string1, string2 )

if string1 > string2 ; result >0

if string1 < string2 ; result<0

if string1 = string2 ; result=0

Code Snippet

 int main()
{
  string1 = " Apple";
  string2 = " Banana";

  int result = strcoll( string1, string2 );

  if ( result1 > 0 )
   cout<<" String1 is greater tan string2 "<<endl;

  else if ( result < 0 0
     cout<<" String2 is greater than string1 " <<endl;

  else
    cout << " String1 is equal to string2 " <<endl;
}

Stoll() in C++

This function converts a string to an long long integer. The string to be converted is passed as an argument.

Syntax :-               stoll ( string_name , nullptr, base );

Code Snippet

 int main()
{
   string s = " 2301897 ";
  
   cout<< stoll ( s, nullptr, 10 );
}

It will print the output as 2301897.

The size of string is more than int so to read large numbers we can use strings and later on we can convert it into long long int using stoll().

Thanks For Reading some useful built-in string functions in C++.

 We will discuss a few more useful string functions in the upcoming post. Keep reading and stay updated.

Read also,

Leave a Reply

Your email address will not be published. Required fields are marked *