C Programs Tutorials | IT Developer
IT Developer

C Programming - C String Functions



Share with a Friend

C Programming - C String Functions

C String Functions

In C, strings are arrays of characters terminated by a null character (\0). The C Standard Library provides a collection of functions for manipulating strings, which are declared in the header file <string.h>.

Here is a list of some commonly used string functions in C:

  1. strlen()
  • Purpose: Calculates the length of a string (excluding the null character \0).
  • Syntax:

C

size_t strlen(const char *str);

  • Example:

C

#include <stdio.h>

#include <string.h>

int main() {

    char str[] = "Hello, World!";

    printf("Length of the string: %zu\n", strlen(str));  // Output: 13

    return 0;

}

  1. strcpy()
  • Purpose: Copies a string from the source to the destination.
  • Syntax:

C

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

  • Example:

C

#include <stdio.h>

#include <string.h>

int main() {

    char src[] = "Hello";

    char dest[20];

    strcpy(dest, src);

    printf("Destination string: %s\n", dest);  // Output: Hello

    return 0;

}

  1. strncpy()
  • Purpose: Copies a specified number of characters from the source to the destination.
  • Syntax:

C

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

  • Example:

C

#include <stdio.h>

#include <string.h>

int main() {

    char src[] = "Hello, World!";

    char dest[20];

    strncpy(dest, src, 5);  // Copies first 5 characters<

    dest[5] = '\0';  // Null-terminate the string manually

    printf("Destination string: %s\n", dest);  // Output: Hello

    return 0;

}

  1. strcat()
  • Purpose: Concatenates (appends) one string to the end of another.
  • Syntax:

C

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

  • Example:

C

#include <stdio.h>

#include <string.h>

int main() {

    char str1[20] = "Hello, ";

    char str2[] = "World!";

    strcat(str1, str2);  // Concatenate str2 to str1

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

    return 0;

}

  1. strncat()
  • Purpose: Concatenates a specified number of characters from the source string to the destination string.
  • Syntax:

C

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

  • Example:

C

#include <stdio.h>

#include <string.h>

int main() {

    char str1[20] = "Hello, ";

    char str2[] = "World!";

    strncat(str1, str2, 3);  // Concatenate first 3 characters of str2 to str1

    printf("Concatenated string: %s\n", str1);  // Output: Hello, Wor

    return 0;

}

  1. strcmp()
  • Purpose: Compares two strings lexicographically (dictionary order).
    • Returns 0 if both strings are equal.
    • Returns a negative value if the first string is lexicographically less than the second.
    • Returns a positive value if the first string is lexicographically greater than the second.
  • Syntax:

C

int strcmp(const char *str1, const char *str2);

  • Example:

C

#include <stdio.h>

#include <string.h>

int main() {

    char str1[] = "apple";

    char str2[] = "banana";

    int result = strcmp(str1, str2);

    if (result == 0)

        printf("Strings are equal.\n");

    else if (result < 0)

        printf("'%s' is less than '%s'.\n", str1, str2);

    else

        printf("'%s' is greater than '%s'.\n", str1, str2);

    return 0;

}

  1. strncmp()
  • Purpose: Compares the first n characters of two strings.
  • Syntax:

C

int strncmp(const char *str1, const char *str2, size_t n);

  • Example:

C

#include <stdio.h>

#include <string.h>

int main() {

    char str1[] = "apple";

    char str2[] = "apricot";

    int result = strncmp(str1, str2, 3);  // Compare first 3 characters

    if (result == 0)

        printf("The first 3 characters are equal.\n");

    else

        printf("The first 3 characters are different.\n");

    return 0;

}

  1. strchr()
  • Purpose: Finds the first occurrence of a character in a string.
  • Syntax:

C

char *strchr(const char *str, int c);

  • Example:

C

#include <stdio.h>

#include <string.h>

int main() {

    char str[] = "Hello, World!";

    char *ch = strchr(str, 'o');

    if (ch)

        printf("First occurrence of 'o' is at position: %ld\n", ch - str);

    else

        printf("'o' not found.\n");

    return 0;

}

  1. strrchr()
  • Purpose: Finds the last occurrence of a character in a string.
  • Syntax:

C

char *strrchr(const char *str, int c);

  • Example:

C

#include <stdio.h>

#include <string.h>

int main() {

    char str[] = "Hello, World!";

    char *ch = strrchr(str, 'o');

    if (ch)

        printf("Last occurrence of 'o' is at position: %ld\n", ch - str);

    else

        printf("'o' not found.\n");

    return 0;

}

  1. strstr()
  • Purpose: Finds the first occurrence of a substring within a string.
  • Syntax:

C

char *strstr(const char *haystack, const char *needle);

  • Example:

C

#include <stdio.h>

#include <string.h>

int main() {

    char str[] = "Hello, World!";

    char *substr = strstr(str, "World");

    if (substr)

        printf("Substring found at position: %ld\n", substr - str);

    else

        printf("Substring not found.\n");

    return 0;

}

  1. strtok()
  • Purpose: Tokenizes a string into smaller substrings based on a delimiter.
  • Syntax:

C

char *strtok(char *str, const char *delim);

  • Example:

C

#include <stdio.h>

#include <string.h>

int main() {

    char str[] = "Hello, World, C, Programming";

    char *token = strtok(str, ", ");

    while (token != NULL) {

        printf("%s\n", token);

        token = strtok(NULL, ", ");

    }

    return 0;

}

  1. strdup() (Non-Standard)
  • Purpose: Duplicates a string by allocating memory and copying the contents of the string.
  • Syntax:

C

char *strdup(const char *str);

  • Example:

C

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

int main() {

    char str[] = "Hello, World!";

    char *copy = strdup(str);

    printf("Original: %s\n", str);

    printf("Duplicate: %s\n", copy);

    free(copy);  // Don't forget to free the allocated memory

    return 0;

}

Summary:

C provides a range of string manipulation functions that allow you to perform tasks like string comparison, concatenation, searching, and more. Understanding and using these functions effectively can help you manage and manipulate strings in C programs. Make sure to include the <string.h> header file to access these functions.