Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

String Functions in C

Introduction

In C programming, a string is a sequence of characters terminated with a null character '\0'. The C language provides a variety of string functions that allow you to perform operations on strings such as copying, concatenation, comparison, and more. In this tutorial, we will cover some of the most commonly used string functions available in the C Standard Library.

strlen()

The strlen() function returns the length of a string, excluding the null character.

Example:

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

int main() {
    char str[] = "Hello, World!";
    printf("Length of the string is: %lu\n", strlen(str));
    return 0;
}

Output:

Length of the string is: 13

strcpy()

The strcpy() function copies the source string into the destination string. The destination string must be large enough to contain the source string.

Example:

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

int main() {
    char dest[20];
    char src[] = "Hello, World!";
    strcpy(dest, src);
    printf("Destination string: %s\n", dest);
    return 0;
}

Output:

Destination string: Hello, World!

strcat()

The strcat() function concatenates (appends) the source string to the destination string.

Example:

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

int main() {
    char dest[50] = "Hello";
    char src[] = ", World!";
    strcat(dest, src);
    printf("Concatenated string: %s\n", dest);
    return 0;
}

Output:

Concatenated string: Hello, World!

strcmp()

The strcmp() function compares two strings. It returns an integer less than, equal to, or greater than zero if the first string is found, respectively, to be less than, to match, or be greater than the second string.

Example:

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

int main() {
    char str1[] = "Hello";
    char str2[] = "World";
    int result = strcmp(str1, str2);
    if(result < 0) {
        printf("%s is less than %s\n", str1, str2);
    } else if(result > 0) {
        printf("%s is greater than %s\n", str1, str2);
    } else {
        printf("%s is equal to %s\n", str1, str2);
    }
    return 0;
}

Output:

Hello is less than World

strncpy()

The strncpy() function copies up to n characters from the source string to the destination string. If the source string is less than n characters long, the remainder of the destination string is filled with null characters.

Example:

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

int main() {
    char dest[20];
    char src[] = "Hello, World!";
    strncpy(dest, src, 5);
    dest[5] = '\0';  // Ensure null-termination
    printf("Destination string: %s\n", dest);
    return 0;
}

Output:

Destination string: Hello

strncat()

The strncat() function appends up to n characters from the source string to the destination string, and then adds a null character.

Example:

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

int main() {
    char dest[20] = "Hello";
    char src[] = ", World!";
    strncat(dest, src, 7);
    printf("Concatenated string: %s\n", dest);
    return 0;
}

Output:

Concatenated string: Hello, World!

strstr()

The strstr() function finds the first occurrence of the substring within a string. If the substring is found, it returns a pointer to the beginning of the substring; otherwise, it returns NULL.

Example:

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

int main() {
    char str[] = "Hello, World!";
    char substr[] = "World";
    char *result = strstr(str, substr);
    if(result) {
        printf("Substring found: %s\n", result);
    } else {
        printf("Substring not found\n");
    }
    return 0;
}

Output:

Substring found: World!

strchr()

The strchr() function returns a pointer to the first occurrence of a character in a string. If the character is not found, it returns NULL.

Example:

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

int main() {
    char str[] = "Hello, World!";
    char ch = 'W';
    char *result = strchr(str, ch);
    if(result) {
        printf("Character found: %s\n", result);
    } else {
        printf("Character not found\n");
    }
    return 0;
}

Output:

Character found: World!

strrchr()

The strrchr() function returns a pointer to the last occurrence of a character in a string. If the character is not found, it returns NULL.

Example:

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

int main() {
    char str[] = "Hello, World!";
    char ch = 'o';
    char *result = strrchr(str, ch);
    if(result) {
        printf("Last occurrence of character: %s\n", result);
    } else {
        printf("Character not found\n");
    }
    return 0;
}

Output:

Last occurrence of character: orld!