C String Functions

In C, there are several built-in string functions that can be used to manipulate strings. These functions are included in the string.h header file and can be used to perform operations such as copying, concatenating, comparing, and searching strings.

Some common string functions in C are:

  • strlen(): Used to find the length of a string.
  • strcpy(): Used to copy one string to another.
  • strcat(): Used to concatenate two strings.
  • strcmp(): Used to compare two strings.
  • strstr(): Used to search for a substring within a string.


#include <stdio.h>
#include <string.h>

int main() {
   char str1[20] = "Hello";
   char str2[20] = "World";
   char str3[20];
   // Copy str1 to str3
   strcpy(str3, str1);
   printf("str3 after copying str1: %s\n", str3);
   // Concatenate str2 to str3
   strcat(str3, str2);
   printf("str3 after concatenating str2: %s\n", str3);
   // Compare str1 and str2
   int result = strcmp(str1, str2);
   if (result < 0) {
      printf("str1 is less than str2\n");
   } else if (result > 0) {
      printf("str1 is greater than str2\n");
   } else {
      printf("str1 is equal to str2\n");
   // Find "lo" in str3
   char* substring = strstr(str3, "lo");
   printf("Substring found: %s\n", substring);
   return 0;

This program will output the following:

str3 after copying str1: Hello
str3 after concatenating str2: HelloWorld
str1 is less than str2
Substring found: loWorld

String functions are an important tool for working with text data in C. They can simplify common string operations and save time in developing complex programs.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial