DEV Community

Sujith V S
Sujith V S

Posted on

String functions in C programming.

To use the string functions, we have to use <string.h> header file.

strlen()

#include <string.h>

int main() {
    char str[] = "Hello, World!";
    int length = strlen(str);
    printf("Length of the string: %d\n", length);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

strcpy()

This function is used to copy one string to another.

#include <string.h>

int main() {
    char food[] = "Pizza";
    char bestFood[strlen(food)];
    strcpy(bestFood, food);
    printf("%s", bestFood);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

strcpy(bestFood, food);
Here first argument is the variable we want to put the copied string. Second argument is the variable in which we want to copy the string.

strcat()

This is used to join two string together.

#include <string.h>

int main() {
    char str1[] = "Hello, ";
    char str2[] = "world!";

    strcat(str1, str2);

    printf("%s", str1);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

To concatenate second string to the first string.

strcmp()

String comparison function to check if two strings are equal. If this function returns 0 then it can be considered as equal.

#include <string.h>

int main() {
    char text1[] = "abcd";
    char text2[] = "abcd";

    int result = strcmp(text1, text2);

    printf("%d", result);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

These are just a few examples, and there are more string manipulation functions available in the C standard library <string.h>

Top comments (1)

Collapse
 
vahidn profile image
Vahid Nasiri

You should start implementing banned APi github.com/x509cert/banned/tree/ma...