String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions

Article by: Manish Methani

Last Updated: September 29, 2021 at 2:04pm IST

In previous tutorial ,we have seen strings functions like strlen, strcpy,strcmp,strcat in C Language. If you havent checked it, go for it here.

Lets begin with the today's topic of discussion on strcat, strncat, strcpy, strncpy, strchr, strrchr, strstr string functions. 

strcat

It concatenates two strings and returns the concatenated string.

#include <stdio.h>
#include <string.h>
int main()
{
     char s1[10] = "Hello";
     char s2[10] = "World";
     strcat(s1,s2);
     printf("Output string after concatenation: %s", s1);
     return 0;
}

Output:

Output string after concatenation: HelloWorld
 
strncat

It concatenates n characters of str2 to string str1. A terminator char (‘’) will always be appended at the end of the concatenated string.

#include <stdio.h>
#include <string.h>
int main()
{
     char s1[10] = "Hello";
     char s2[10] = "World";
     strncat(s1,s2, 3);
     printf("Concatenation using strncat: %s", s1);
     return 0;
}

Output:

Concatenation using strncat: HelloWor

strcpy

It copies the string str2 into string str1, including the end character (terminator char ‘’).

#include <stdio.h>
#include <string.h>
int main()
{
     char s1[30] = "string 1";
     char s2[30] = "string 2 : I’m gonna copied into s1";
     /* this function has copied s2 into s1*/
     strcpy(s1,s2);
     printf("String s1 is: %s", s1);
     return 0;
}

Output:

String s1 is: string 2: I’m gonna copied into s1

strncpy

It copies up to n characters from the string pointed to, by src to dest. Here we have used function memset() to clear the memory location.

#include <stdio.h>
#include <string.h>
int main()
{
   char src[40];
   char dest[12];
  
   memset(dest, "", sizeof(dest));
   strcpy(src, "It feels cool to write a code");
   strncpy(dest, src, 10);

   printf("Destination d string : %s
", dest);
   
   return(0);
}

Output

Destination d string : It feels c

strchr

This function is used to search the string from the given character

#include <stdio.h>
#include <string.h>
int main()
{
     char mystr[] = "I’m an example of function strchr";
     printf ("%s", strchr(mystr, 'f'));
     return 0;
}

Output:

f function strchr

strrchr

This function is used to search the string from the given character but in the reverse direction.

#include <stdio.h>
#include <string.h>
int main()
{
     char mystr[] = "I’m an example of function strchr";
     printf ("%s", strrchr(mystr, 'f'));
     return 0;
} 

Output:

function strchr

strstr

It is similar to strchr, except that it searches for string ‘search term’ instead of a single char.

#include <stdio.h>
#include <string.h>
int main()
{
  char s1[] = "codzify.com";
    char s2[] = "codzify";
    char* p;
  
    // Find first occurrence of s2 in s1
    p = strstr(s1, s2);
  
    // Prints the result
    if (p) {
        printf("First occurrence of string '%s' in '%s' is ]'%s'", s2, s1, p);
    } else
        printf("String not found
");
  
    return 0;
}

Output:

First occurrence of string 'codzify' in 'codzify.com' is 'codzify.com'

Discover My FlutterFlow Courses and Template Apps

FlutterFlow Course: Basic to Advanced (2025) – Dating App
Learn to Create the Dating App with Admin Dashboard using No-Code Tool FlutterFlow in this comprehensive course in 2025.
FlutterFlow Course: Learn to Build the Grocery Delivery App
Learn to Create Grocery-Delivery App in FlutterFlow, ideal for anyone looking to implement a functional E-Commerce App.
FlutterFlow Course: Learn to Build the Online Courses App
Learn to Create Online Courses App in FlutterFlow, ideal for anyone looking to implement a functional Courses App.
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com