Skip to content
Related Articles

Related Articles

Improve Article

Commonly used String functions in C/C++ with Examples

  • Difficulty Level : Easy
  • Last Updated : 29 Aug, 2021

Strings in C: Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. 
Some of the most commonly used String functions are:

  • strcat: The strcat() function will append a copy of the source string to the end of destination string. The strcat() function takes two arguments: 
    1) dest 
    2) src 
    It will append copy of the source string in the destination string. The terminating character at the end of dest is replaced by the first character of src . 
    Return value: The strcat() function returns dest, the pointer to the destination string. 

CPP




// CPP program to demonstrate
// strcat
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
    char dest[50] = "This is an";
    char src[50] = " example";
 
    strcat(dest, src);
    cout << dest;
    return 0;
}
Output: 
This is an example

 

  • strrchr: In C/C++, strrchr() is a predefined function used for string handling. cstring is the header file required for string functions.
    This function Returns a pointer to the last occurrence of a character in a string. 
    The character whose last occurrence we want to find in passed as the second argument to the function and the string in which we have to find the character is passed as the first argument to the function. 
    Syntax 
char *strrchr(const char *str, int c) 
  • Here, str is the string and c is the character to be located. It is passed as its int promotion, but it is internally converted back to char.
    Example: 

C++




// C++ code to demonstrate the working of
// strrchr()
#include <cstring>
#include <iostream>
using namespace std;
 
// Driver function
int main()
{
 
    // initializing variables
    char st[] = "GeeksforGeeks";
    char ch = 'e';
    char* val;
 
    // Use of strrchr()
    // returns "ks"
    val = strrchr(st, ch);
 
    cout <<"String after last " << ch << " is :  " << val << endl;
 
    char ch2 = 'm';
 
    // Use of strrchr()
    // returns null
    // test for null
    val = strrchr(st, ch2);
 
    cout <<"String after last " << ch2 << " is :  " << val << endl;
 
    return (0);
}
 
// This code is contributed by shivanisinghss2110

C




// C code to demonstrate the working of
// strrchr()
 
#include <stdio.h>
#include <string.h>
 
// Driver function
int main()
{
 
    // initializing variables
    char st[] = "GeeksforGeeks";
    char ch = 'e';
    char* val;
 
    // Use of strrchr()
    // returns "ks"
    val = strrchr(st, ch);
 
    printf("String after last %c is :  %s \n",
           ch, val);
 
    char ch2 = 'm';
 
    // Use of strrchr()
    // returns null
    // test for null
    val = strrchr(st, ch2);
 
    printf("String after last %c is :  %s ",
           ch2, val);
 
    return (0);
}
Output: 
String after last e is :  eks 
String after last m is :  (null)

 

  • strcmp: strcmp() is a built-in library function and is declared in <string.h> header file. This function takes two strings as arguments and compare these two strings lexicographically.
    Syntax:
int strcmp(const char *leftStr, const char *rightStr );
  • In the above prototype, function srtcmp takes two strings as parameters and returns an integer value based on the comparison of strings. 
    • strcmp() compares the two strings lexicographically means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered.
    • If first character in both strings are equal, then this function will check the second character, if this is also equal then it will check the third and so on
    • This process will be continued until a character in either string is NULL or the characters are unequal.
  • strcpy: strcpy() is a standard library function in C/C++ and is used to copy one string to another. In C it is present in string.h header file and in C++ it is present in cstring header file. 
    Syntax: 
char* strcpy(char* dest, const char* src);
  • Parameters: This method accepts following parameters: 
    • dest: Pointer to the destination array where the content is to be copied. 
       
    • src: string which will be copied. 
       
  • strlen: The strlen() function calculates the length of a given string.The strlen() function is defined in string.h header file. It doesn’t count null character ‘\0’.
    Syntax: 
int strlen(const char *str);
  • Parameter: 
    • str: It represents the string variable whose length we have to find.
  • strncat: In C/C++, strncat() is a predefined function used for string handling. string.h is the header file required for string functions.
    This function appends not more than n characters from the string pointed to by src to the end of the string pointed to by dest plus a terminating Null-character. The initial character of string(src) overwrites the Null-character present at the end of string(dest). Thus, length of the string(dest) becomes strlen(dest)+n. But, if the length of the string(src) is less than n, only the content up to the terminating null-character is copied and length of the string(dest) becomes strlen(src) + strlen(dest).
    The behavior is undefined if 
    • the strings overlap.
    • the dest array is not large enough to append the contents of src.
    • dest: the string where we want to append.
    • src: the string from which ‘n’ characters are going to append.
    • n: represents maximum number of character to be appended. size_t is an unsigned integral type.
  • strncmp: std::strncmp() function lexicographically compares not more than count characters from the two null-terminated strings and returns an integer based on the outcome. 
    • This function takes two strings and a number num as arguments and compare at most first num bytes of both the strings.
    • num should be at most equal to the length of the longest string. If num is defined greater than the string length than comparison is done till the null-character(‘\0’) of either string.
    • This function compares the two strings lexicographically. It starts comparison from the first character of each string. If they are equal to each other, it continues and compare the next character of each string and so on.
    • This process of comparison stops until a terminating null-character of either string is reached or num characters of both the strings matches.
  • strncpy: The strncpy() function is similar to strcpy() function, except that at most n bytes of src are copied. If there is no NULL character among the first n character of src, the string placed in dest will not be NULL-terminated. If the length of src is less than n, strncpy() writes additional NULL character to dest to ensure that a total of n character are written.
    Syntax: 
char *strncpy( char *dest, const char *src, size_t n )
  • Parameters: This function accepts two parameters as mentioned above and described below: 
    • src: The string which will be copied.
    • dest: Pointer to the destination array where the content is to be copied.
    • n: The first n character copied from src to dest.
  • strrchr: The strrchr() function in C/C++ locates the last occurrence of a character in a string. It returns a pointer to the last occurrence in the string. The terminating null character is considered part of the C string. Therefore, it can also be located to retrieve a pointer to the end of a string. It is defined in cstring header file. 
    Syntax : 
const char* strrchr( const char* str, int ch )
            or
char* strrchr( char* str, int ch )
  • Parameter :The function takes two mandatory parameters which are described below: 
    • str : specifies the pointer to the null terminated string to be searched for.
    • ch: specifies the character to be search for.
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :