strcat, strncat, strcmp, strncmp, strchr, strrchr library functions


Advertisements

Share

Article background

In this article I will demonstrate the use of strcat, strncat, strcmp, strncmp, strchr, strrchr library functions using example code snippets.

Library Functions


strcat library function

The strcat() function appends the src string to the dest string, overwriting the terminating null byte at the end of dest, and then adds a terminating null byte. The strings may not overlap in memory and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable.

The function strcat is declared as the following.

  char *strcat(char *dest, const char *src);

To demonstarte the right usage of strcat library function we have written program below. In this program the source string is concatenated to the destination string and the result string is stored in the destination string.

strcat library function demonstartion example

#include "stdio.h"
#include "string.h"
int main() {
  char strs[] = "Bangalore";
  char strd[] = "2134";
  strcat(strd, strs);
  printf("strd is %s\n",strd);
  return 0;
}
Output of the above program is given below.
$ gcc prog.c
$ ./a.out
strd is 2134Bangalore
$

strncat library function demonstartion example

The function strncat is declared as the following.

  char *strncat(char *dest, const char *src, size_t n);

To demonstarte the right usage of strncat library function we have written program below. In this program an empty string is declared as the destination string and then use strncat to concatenate the required number of characters to the destination string.

strncat library function demonstration example

#include "stdio.h"
#include "string.h"
int main() {
  char strs[] = "Bangalore-2134";
  char strd[10];
  strd[0] = '\0';
  strncat(strd, strs, 9);
  printf("strd is %s\n",strd);
  return 0;
}
Output of the above debugging exercise is given below.
$ gcc prog.c
$ ./a.out
strd is Bangalore
$

strcmp library function demonstartion example

strcmp library function is used to compare two strings and is declared in string.h header file. strcmp() returns an integer indicating the result of the comparison. It returns 0, if the s1 and s2 are equal, a negative value if s1 is less than s2, a positive value if s1 is greater than s2.

The function strcmp is declared as the following.

  int strcmp(const char *s1, const char *s2);
strcmp library function demonstartion example

#include "stdio.h"
#include "string.h"
int main() {
  int res;
  char str1[] = "Mbed Technologies";
  char str2[] = "Mbed Technologies";
  char str3[] = "Mbed Technologies";
  char str4[] = "Technologies";
  char str5[] = "Technologies";
  char str6[] = "Mbed Technologies";
  if(strcmp(str1, str2) == 0)
  {
    printf("str1 and str2 strings are equal\n");
  }
  res = strcmp(str3, str4);
  printf("res:%d\n",res);
  res = strcmp(str5, str6);
  printf("res:%d\n",res);  
  return 0;
}
Output of the above program is given below.
$ gcc prog.c
$ ./a.out
str1 and str2 strings are equal
res:-7
res:7
$

strncmp library function demonstartion example

strncmp is similar to strcmp function except that it compares only the first n characters in strings s1 and s2.

The function strcat is declared as the following.

  int strncmp(const char *s1, const char *s2, size_t n);
strncmp library function demonstartion example

#include "stdio.h"
#include "string.h"
int main() {
  char str1[] = "Mbed Technologies";
  char str2[] = "Mbed Articles";
  if(strncmp(str1, str2, strlen("Mbed")) == 0)
  {
    printf("%d bytes of str1 and str2 are equal\n", strlen("Mbed"));
  }  
  return 0;
}
Output of the above program is given below.
$ gcc prog.c
$ ./a.out
4 bytes of str1 and str2 are equal
$

To Read Full Article

Login or Register to create a free Mbed Technologies account

Register Login


Want to contribute or ask a new article? Write and upload your article information .
 | 
Share

 Articles

 C Programming

 Linux Software

 Search Code Snippets

 Popular C search examples

 Popular C search MCQs

 Test your skills:online tests

 C Programming

 Python Programming

 Linux Software

 Quantitative Aptitude

Exam Edge Home page

 Embedded System Software