Monday, 6 February 2017

Concatenate two strings without using strcat function


we can implement our own strcat function


int my_strlen(const char *str){
        int len=0;
        if(str){
                while(str[len] != '\0'){
                        len++;
                }
        }

        return len;
}
char * my_strcat(char *dest, const char *src)
{
        int dest_len = my_strlen(dest);
        if( dest == NULL || src == NULL){
                return dest;
        }
        int src_itr=0;
        while(src[src_itr] != '\0'){
                dest[dest_len] = src[src_itr];
                dest_len++;
                src_itr++;
        }
        dest[dest_len] = '\0';
        return dest;
}

complete code


#include <stdio.h>
#include <string.h>
int my_strlen(const char *str){
        int len=0;
        if(str){
                while(str[len] != '\0'){
                        len++;
                }
        }

        return len;
}
char * my_strcat(char *dest, const char *src)
{
        int dest_len = my_strlen(dest);
        if( dest == NULL || src == NULL){
                return dest;
        }
        int src_itr=0;
        while(src[src_itr] != '\0'){
                dest[dest_len] = src[src_itr];
                dest_len++;
                src_itr++;
        }
        dest[dest_len] = '\0';
        return dest;
}
int main(int argc, char *argv[]){
        char str1[256]="helllo ";
        my_strcat(str1, "world!!!");
        printf("%s\n", str1);
        return 0;
}

Compilation and Output of Program


rajesh@ideapad:~/Rajesh/Blog/string$ gcc own_strcat.c
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out 
helllo world!!!

No comments:

Post a Comment