String concat

How to Insert a string inside of another string from any position and print it.
Without using string header file or functions.
use two arrays of chars to store the two strings

run a for loop from the length of the string you want to insert into to the length of the string you want to insert into + the string you want to insert

for each iteration copy the element from string 2 to string 1

come back with your code, if you didnt succed, we cannot just give you the program
I came up with this.... String 2 is perfectly copying in string 1 in any position of string 1. But if i add it at the last, it doesn't works perfectly...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<stdio.h>
#include<iostream>

using namespace std;

int main()
{
    char str1[50],str2[50];
    int a_len=0,b_len=0;
    int c,d,e,i=0,x;

    cout<<"String1: ";
    gets(str1);
    cout<<"String2: ";
    gets(str2);
    cout<<"Position: ";
    cin>>x;

    while(str1[a_len]!='\0')
    {
        a_len++;
    }
    while(str2[b_len]!='\0')
    {
        b_len++;
    }
    c=a_len;
    d=a_len+b_len+1;
    e=x+b_len-1;
    for(i=0;i<=b_len+2;i++)
    {
        str1[d]=str1[a_len];
        d--;
        a_len--;
    }
    for(i=0;i<b_len;i++)
    {
        str1[x-1]=str2[i];
        x++;
    }
    str1[e]=' ';

    cout<<"Output: ";
    for(i=0;i<c+b_len+1;i++)
    {
        cout<<str1[i];
    }

}
Last edited on
Topic archived. No new replies allowed.