Featured Post

Trie implementation in C

Custom strncat function in C

This is a simple implementation of Variable length String Concatenation function.

It'll concatenate the mentioned length of a string into another string.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char *my_strncat(char *target1,const char *source1,int no); 

int main()
{
    char *source;
    char *target;
    char *ret;
    int no_of_char;

    source=(char*)malloc(10);
    target=(char*)malloc(20);

    printf("enter the string :\n");
    scanf("%s",target);
    printf("enter the string to be concatenated:\n");
    scanf("%s",source);
    printf("\nenter the number of characters to be concatenated:");
    scanf("%d",&no_of_char);

    ret=my_strncat(target,source,no_of_char);

    printf("\nThe copied string is %s \n", ret);

    free(source);
    free(target);

    return EXIT_SUCCESS;
}
char * my_strncat(char *target1,const char *source1,int no)
{
    char *tempt=target1;
    int n=0;
    while(*target1)
    {
        target1++;
    }
    target1--;
    if(*target1!='\n')
    {
        target1++;
    }

    while(n<no)
    {
        *target1=*source1;
        target1++;
        source1++;
        n++;
    }

    *target1='\0';
    return tempt;
}


Comments