DEV Community

As Manjaka Josvah
As Manjaka Josvah

Posted on • Updated on

C Basic methods

We are going to learn about the basic C programming methods and their implementations from scratch

String copy

  1. strcpy
  2. strncpy
  3. strlcpy

String compare

  1. strcmp
  2. strncmp

String concatenate

  1. strcat
  2. strncat
  3. strlcat

I. String copy

Definition

It allows us to copy a source string to another destination string.

  1. strcpy

strcpy is a C standard library function that copies a string from one location to another. It is defined in the string. h header file. The function takes two arguments: a destination buffer where the copied string will be stored, and a source string that will be copied.

It returns the new constructed string (i.e. dest).

Algorithm

  • Set the parameters : char *dest, char *src.
  • Copy all elements in src to dest, i.e. everything inside dest will be removed from 0 to length(src) - 1, and put an "\0" value at the end, when all characters inside src are copied.

Code

char    *ft_strcpy(char *dest, char *src)
{
    int i;

    i = 0;
    while (src[i] != '\0')
    {
        dest[i] = src[i];
        i++;
    }
    dest[i] = '\0';
    return (dest);
}
Enter fullscreen mode Exit fullscreen mode
  1. strncpy

strncpy similar to strcpy but it adds an extra parameter size.

Algorithm

If size is greater than the length of src , the dest result is padded with null characters (\0) up to length count . Return Value. The strncpy() function returns a pointer to dest .

if size > length(src) => append \0 to dest until we reach size.

Code

unsigned int    ft_strlen(char *value)
{
    int             i;
    unsigned int    count;

    i = 0;
    count = 0;
    while (value[i] != '\0')
    {
        count++;
        i++;
    }
    return (count);
}

char    *ft_strncpy(char *dest, char *src, unsigned int n)
{
    unsigned int    i;
    unsigned int    len_src;

    i = 0;
    len_src = ft_strlen(src);
    while (i < n)
    {
        if (i < len_src)
            dest[i] = src[i];
        else
            dest[i] = '\0';
        i++;
    }
    return (dest);
}
Enter fullscreen mode Exit fullscreen mode
  1. strlcpy

Similar to strncpy, but don't append a lot of null after surpassing the src length.

Algorithm

it checks for both i < size - 1 && i < length(src) before appending src to dest

Code

unsigned    int ft_strlen(char *str)
{
    unsigned int    i;
    int             count;

    i = 0;
    count = 0;
    while (str[i] != '\0')
    {
        count++;
        i++;
    }
    return (count);
}

unsigned    int ft_strlcpy(char *dest, char *src, unsigned int size)
{
    unsigned int    i;
    unsigned int    src_len;

    i = 0;
    src_len = ft_strlen(src);
    while (i < size - 1 && i < src_len)
    {
        dest[i] = src[i];
        i++;
    }
    dest[i] = '\0';
    return (src_len);
}
Enter fullscreen mode Exit fullscreen mode

II. String compare

Definition

It allows us to compare two strings.

  1. strcmp

The strcmp function is a critical part of the C language, defined and prototyped in the string. h header file. It is used for comparing two character arrays or strings lexicographically. This means that it checks every character at every index in both strings for equality.

Algorithm

  • Set the parameters : char *str1, char *str2.
  • compare each elements in str1 with str2.
  • If an element in str1 is not equal to str2, the comparison process stops, and it returns the difference between the 2 different elements of str1 and str2.

The return value from strcmp is 0 if the two strings are equal, less than 0 if str1 compares less than str2 , and greater than 0 if str1 compares greater than str2 . No other assumptions should be made about the value returned by strcmp.

Code

#include <unistd.h>

int ft_strcmp(char *s1, char *s2)
{
    while (*s1 && *s2 && *s1 == *s2)
    {
        s1++;
        s2++;
    }
    return (*s1 - *s2);
}
Enter fullscreen mode Exit fullscreen mode
  1. strncmp

strncmp is a function in C which is used to compare two array of characters upon N indexes and return if the first array is less, equal or greater than the second array. It overcomes the limitations of strcmp function by allowing programmers to set the number of characters to compare.

Algorithm

  • Set the parameters : char *str1, char *str2.
  • compare each elements in str1 with str2.
  • If an element in str1 is not equal to str2, the comparison process stops, and it returns the difference between the 2 different elements of str1 and str2.

The return value from strcmp is 0 if the two strings are equal, less than 0 if str1 compares less than str2 , and greater than 0 if str1 compares greater than str2 . No other assumptions should be made about the value returned by strcmp.

code

int ft_strncmp(char *s1, char *s2, unsigned int n)
{
    if (n == 0)
        return (0);
    while (*s1 && *s2 && *s1 == *s2 && --n)
    {
        s1++;
        s2++;
    }
    return (*s1 - *s2);
}
Enter fullscreen mode Exit fullscreen mode

III. String concatenate

Definition

String concatenation in C can be simply defined as the addition of two strings or updating them. In C language we can add two or more strings end to end or can update them using the concatenation process. The concatenation helps to combine two or more strings together and provide them as a single string value.

  1. strcat

The strcat() function concatenates string2 to string1 and ends the resulting string with the null character. The strcat() function operates on null-ended strings. The string arguments to the function should contain a null character (\0) that marks the end of the string. No length checking is performed.

Algorithm

char    *ft_strcat(char *str1, char *str2)
{
    int i;

    i = 0;
    while (str1[i] != '\0')
    {
        i++;
    }
    while (*str2)
    {
        str1[i++] = *str2;
        str2++;
    }
    str1[i] = '\0';
    return (str2);
}
Enter fullscreen mode Exit fullscreen mode
  1. strncat

The strncat() function appends the first count characters of string2 to string1 and ends the resulting string with a null character (\0). If count is greater than the length of string2, the length of string2 is used in place of count.
if (size > length(str2)) => size = length(str2).

Algorithm

char    *ft_strncat(char *dest, char *src, unsigned int nb)
{
    int i;

    i = 0;
    while (dest[i] != '\0')
        i++;
    while (*src && nb--)
    {
        dest[i] = *src;
        src++;
        i++;
    }
    dest[i] = '\0';
    return (dest);
}
Enter fullscreen mode Exit fullscreen mode
  1. strlcat

The logical algorithm BTS

Image description

Image description

Algorithm

char    *ft_strlcat(char *dest, char *src, unsigned int nb)
{
    int i;
    int len;

    i = 0;
    while (dest[i] != '\0')
        i++;
    while (*src && --nb)
    {
        dest[i] = *src;
        src++;
        i++;
    }
    dest[i] = '\0';
    if (strlen(src) < nb)
        len = strlen(dest) + strlen(src);
    else
        len = strlen(dest) + nb;
    return (len);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)