Solved programming problem

Create a new String with two old string with pointer in C.

Source code

#include<stdio.h>

#include<string.h>

char *concatName(char *str1,char *str2,char *result,int n,int m)

{

    int i=0,j=0;

    while(i<n) ///save 1st part

    {

        *(result+j) = *(str1+i);

        i++;

        j++;

    }

    i=0;

    while(i<m) ///save 2nd part

    {

        *(result+j) = *(str2+i);

        i++;

        j++;

    }

    *(result+j)='';

    return result; ///return 3rd string position

}

int main()

{

    int n,m;

    char s1[100],s2[100],s3[100];

    char *str1,*str2,*result;

    printf("Please Enter 1st String: ");

    gets(s1);

    printf("Please Enter 2nd String: ");

    gets(s2);

    str1 = s1; ///point all 3 string in 3 pointer

    str2 = s2;

    result = s3;

    printf("Please Enter Two Position: ");

    scanf("%d %d",&n,&m);

    char *r = concatName(str1,str2,result,n,m); ///call the function

    int i=0;

    printf("Your New String: ");

    while(*(r+i)!='')

    {

        printf("%c",*(r+i));

        i++;

    }

    return 0;

}

Output

Please Enter 1st String: abcdef
Please Enter 2nd String: abcdef
Please Enter Two Position: 3 3
Your New String: abcabc

Join the discussion

Add your comment