Skip to content

String functions in C

In C, string functions are available in the standard library <string.h>. These functions provide a wide range of operations for manipulating strings, such as copying, concatenating, comparing, searching, and tokenizing strings. Let’s discuss some of the most commonly used string functions in detail:

1. strlen():

  • Purpose: Returns the length of a string.
  • Syntax: size_t strlen(const char *str);
  • Example:

#include <stdio.h>

#include <string.h>

int main() {

    char str[] = “Hello”;

    size_t length = strlen(str);

    printf(“Length of string: %zu\n”, length); // Output: 5

    return 0;

}

2. strcpy():

  • Purpose: Copies one string to another.
  • Syntax: char *strcpy(char *dest, const char *src);
  • Example:

#include <stdio.h>

#include <string.h>

int main() {

    char source[] = “Hello”;

    char destination[10];

    strcpy(destination, source);

    printf(“Copied string: %s\n”, destination); // Output: Hello

    return 0;

}

3. strcat():

  • Purpose: Concatenates two strings.
  • Syntax: char *strcat(char *dest, const char *src);
  • Example:

#include <stdio.h>

#include <string.h>

int main() {

    char str1[20] = “Hello”;

    char str2[] = ” World!”;

    strcat(str1, str2);

    printf(“Concatenated string: %s\n”, str1); // Output: Hello World!

    return 0;

}

4. strcmp():

  • Purpose: Compares two strings.
  • Syntax: int strcmp(const char *str1, const char *str2);
  • Returns:
    • 0 if both strings are equal.
    • A value less than 0 if str1 is less than str2.
    • A value greater than 0 if str1 is greater than str2.
  • Example:

#include <stdio.h>

#include <string.h>

int main() {

    char str1[] = “Hello”;

    char str2[] = “World”;

    int result = strcmp(str1, str2);

    if (result == 0) {

        printf(“Strings are equal\n”);

    } else if (result < 0) {

        printf(“str1 is less than str2\n”);

    } else {

        printf(“str1 is greater than str2\n”);

    }

    return 0;

}

5. strchr():

  • Purpose: Finds the first occurrence of a character in a string.
  • Syntax: char *strchr(const char *str, int c);
  • Returns:
    • A pointer to the first occurrence of c in str.
    • NULL if c is not found.
  • Example:

#include <stdio.h>

#include <string.h>

int main() {

    char str[] = “Hello, World!”;

    char *ptr = strchr(str, ‘W’);

    if (ptr != NULL) {

        printf(“Character found at position: %ld\n”, ptr – str); // Output: Character found at position: 7

    } else {

        printf(“Character not found\n”);

    }

    return 0;

}

6. strstr():

  • Purpose: Finds the first occurrence of a substring in a string.
  • Syntax: char *strstr(const char *str, const char *substr);
  • Returns:
    • A pointer to the first occurrence of substr in str.
    • NULL if substr is not found.
  • Example:

#include <stdio.h>

#include <string.h>

int main() {

    char str[] = “Hello, World!”;

    char *ptr = strstr(str, “World”);

    if (ptr != NULL) {

        printf(“Substring found at position: %ld\n”, ptr – str); // Output: Substring found at position: 7

    } else {

        printf(“Substring not found\n”);

    }

    return 0;

}

These are just a few examples of the many string functions available in C. Familiarizing yourself with these functions can greatly simplify string manipulation tasks in your C programs.