String handling functions in C language

C provides various built-in functions for handling strings. Here is a table of the most commonly used string handling functions in C:

Function
Description
strlen()
Returns the length of a string
strcpy()
Copies one string to another
strcat()
Concatenates (joins) two strings
strncat()
Concatenates a specified number of characters from one string to another
strcmp()
Compares two strings
strncmp()
Compares a specified number of characters from two strings
strchr()
Searches for the first occurrence of a character in a string
strstr()
Searches for the first occurrence of a substring in a string
atoi()
Converts a string to an integer

strlen(): Returns the length of a string.

Example:

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

int main() {
    char str[] = "Hello, world!";
    int len = strlen(str);
    printf("The length of the string is %d\n", len);
    return 0;
}

Output:

The length of the string is 13

strcpy(): Copies one string to another.

Example:

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

int main() {
    char str1[] = "Hello, world!";
    char str2[50];
    strcpy(str2, str1);
    printf("Copied string is: %s\n", str2);
    return 0;
}

Output:

Copied string is: Hello, world!

strncpy(): Copies a specified number of characters from one string to another.

Example:

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

int main() {
    char str1[] = "Hello, world!";
    char str2[50];
    strncpy(str2, str1, 5);
    printf("Copied string is: %s\n", str2);
    return 0;
}

Output:

Copied string is: Hello

strcat(): Concatenates (joins) two strings.

Example:

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

int main() {
    char str1[] = "Hello";
    char str2[] = "world!";
    strcat(str1, str2);
    printf("Concatenated string is: %s\n", str1);
    return 0;
}

Output:

Concatenated string is: Helloworld!

strncat(): Concatenates a specified number of characters from one string to another.

Example:

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

int main() {
    char str1[] = "Hello";
    char str2[] = "world!";
    strncat(str1, str2, 3);
    printf("Concatenated string is: %s\n", str1);
    return 0;
}

Output:

Concatenated string is: Helloworld

strcmp(): Compares two strings.

Example:

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

int main() {
   char str1[] = "apple";
   char str2[] = "banana";

   int result = strcmp(str1, str2);

   if (result < 0) {
      printf("%s comes before %s in dictionary\n", str1, str2);
   } else if (result > 0) {
      printf("%s comes after %s in dictionary\n", str1, str2);
   } else {
      printf("Both strings are equal\n");
   }

   return 0;
}

Output:

apple comes before banana in dictionary

strncmp(): Compares a specified number of characters from two strings

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

int main() {
   char str1[] = "apple pie";
   char str2[] = "apple juice";

   int result = strncmp(str1, str2, 5);

   if (result < 0) {
      printf("%s comes before %s in dictionary\n", str1, str2);
   } else if (result > 0) {
      printf("%s comes after %s in dictionary\n", str1, str2);
   } else {
      printf("Both strings are equal up to 5 characters\n");
   }

   return 0;
}

Output:

Both strings are equal up to 5 characters

strchr(): Searches for the first occurrence of a character in a string

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

int main() {
   char str[] = "hello world";
   char *result = strchr(str, 'w');

   if (result != NULL) {
      printf("The character 'w' was found at position %d\n", result - str + 1);
   } else {
      printf("The character 'w' was not found\n");
   }

   return 0;
}

Output:

The character 'w' was found at position 7

strstr(): Searches for the first occurrence of a substring in a string

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

int main() {
   char str1[] = "The quick brown fox jumps over the lazy dog";
   char str2[] = "brown";

   char *result = strstr(str1, str2);

   if (result != NULL) {
      printf("The substring '%s' was found at position %d\n", str2, result - str1 + 1);
   } else {
      printf("The substring '%s' was not found\n", str2);
   }

   return 0;
}

Output:

The substring 'brown' was found at position 11

atoi(): Converts a string to an integer

#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[] = "123";
    int num = atoi(str);
    printf("The integer is: %d\n", num);
    return 0;
}

Output:

The integer is: 123