Common String Functions strlen strcpy strcmp strcat in C

Learn common string functions in C: strlen, strcpy, strcmp, strcat with examples on Debian 12 using Vim.

Common String Functions in C Programming

C provides several standard functions to manipulate strings, declared in .

strlen

Returns the length of a string (excluding the null terminator).

size_t strlen(const char *str);

strcpy

Copies one string to another.

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

strcmp

Compares two strings lexicographically.

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

strcat

Appends one string to the end of another.

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

Example Program

#include 
#include 

int main() 
    char str1[20] = \\"Hello\\";
    char str2[] = \\"World\\";

    printf(\\"Length of str1: %lu\
\\", strlen(str1));

    strcpy(str1, \\"Hi\\");
    printf(\\"After strcpy: %s\
\\", str1);

    int cmp = strcmp(str1, str2);
    printf(\\"strcmp result: %d\
\\", cmp);

    strcat(str1, str2);
    printf(\\"After strcat: %s\
\\", str1);

    return 0;

Compile and Run

gcc string_functions.c -o string_functions
./string_functions

Expected Output

Length of str1: 5
After strcpy: Hi
strcmp result: negative_value
After strcat: HiWorld

Summary

These string functions help manage and manipulate strings easily in C.

Subscribe to Our YouTube for More

Download as PDF

https://blog.arashtad.com/updates/common-string-functions-strlen-strcpy-strcmp-strcat-in-c/?feed_id=13226&_unique_id=6896fe6816775

Comments

Popular posts from this blog

Why Smart Contracts Are Revolutionary? (Smart Contracts Use Cases)

A Quick Guide to Tron Network Interaction using Python.

#36 Multi-Signature Wallet Smart Contract (Part 3)