Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 modeExit 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 modeExit 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 modeExit 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 modeExit 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
vahidn profile image
Vahid Nasiri
Software Developer
  • Location
    Iran, Tehran
  • Joined

You should start implementing banned APigithub.com/x509cert/banned/tree/ma...

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

React | Django | ..
  • Joined

More fromSujith V S

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp