1. C Programming for Beginners: Writing Your First Hello World Program in C in 2023 2. Mastering Datatypes in C Programming: A Comprehensive Guide for Beginners in 2023 3. Complete Guide to % Format Specifiers in C for Beginners in 2023 4. A Comprehensive Guide to Operators in C Programming: Learn Everything About Operators in C in 2023 5. Master C Operator Precedence and Associativity in 2023: Definition, Examples, and Code 6. Learn Increment and Decrement Operators in C: A Beginners Guide with Examples - Codzify 7. Mastering Bitwise Operators in C: A Comprehensive Guide with Examples - Codzify 8. Ternary Operators in C Programming: A Beginners Guide (2023) 9. Switch Statements in C 2023: Complete Guide for Beginners 10. Learn Functions in C Programming - A Complete Tutorial for Beginners in 2023 11. Call by Value and Call by Reference in C Programming - A Complete Tutorial for Beginners in 2023 12. Storage Classes in C Programming: A Comprehensive Guide for 2023 13. Arrays in C Programming: A Comprehensive Guide for 2023 14. Basics of 2D Array in C Programming in 2023: Definition, Initialization, and Accessing Elements 15. Beginners Guide to Strings in C Programming with Codes and Examples 16. Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions 17. A Comprehensive Guide to Structures in C Programming in 2023 18. Learn Unions in C Programming with Codes, Examples, and Explanations in 2023 19. Dynamic Memory Allocation in C: Understanding malloc, calloc, realloc, and free Functions 20. Introduction to C Programming 21. Hello world program in C 22. String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions 23. Program for Fibonacci number in C - Codzify.com 24. What is palindrome number in C? 25. Write a C program to find a factorial of a given number 26. Write a C program to check whether a given number is prime or not 27. Write a C program to check whether a given number is an Armstrong number or not. 28. Write a C Program to transpose a matrix 29. C Programming Tutorial: Understanding Comments and Identifiers with Code Examples 30. Master String Handling Functions in C in 2023 - Codzify Topics

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'

Learn Flutter, FlutterFlow,
Firebase & Angular.

Looking to learn how to create the production-ready apps
from scratch? Well, you are at the right place.

₹1299 or $15.56

Learn to create the Dating App using No-Code Tool FlutterFlow

Level: Advanced

Explore Curriculum
₹1299 or $15.56

Flutter Mobile App Development Course

Explore Curriculum
₹1299 or $15.56

The Complete Angular Course

Explore Curriculum
₹1299 or $15.56

Dart Programming for Absolute Beginners

Coming Soon
FREE

Next.js course

Start Watching
FREE

Fundamentals of Computer Programming Languages

Start Watching
FREE

Learn HTML, CSS & Bootstrap

Start Watching

Latest Web Stories

1

Learn how to open WhatsApp using FlutterFlow | Step by Step Guide

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer