How can i unite 2 strings in array?

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
50
51
52
53
54
55
56
57
58
59
#include<iostream>;
using namespace std;

int strlen(char []);
void strcat(char S1[], char S2[], int size_1, int size_2);
const int SIZE = 100;
const int MAX = 100;
int main(){
	int lenght_word1 = 0, lenght_word2 = 0;
	char word1[SIZE];
	char word2[SIZE];
	char word3[MAX];
	cin >> word1;
	lenght_word1 = strlen(word1);
	cin >> word2;
	lenght_word2 = strlen(word2);
	strcat(word1, word2, lenght_word1, lenght_word2);
	


	system("pause");
	return 1;
}

int strlen(char array[]){
	int i = 0;
	int lenght;
	if (array[0] == 0){
		lenght = 0;
	}
	else{
		do{
			i = i + 1;
			lenght = i;
		} while (array[i] != 0);

		}

	return lenght;
}

void strcat(char S1[], char S2[], int size_1, int size_2){
	
	char S3[MAX];

	for (int a = 0; a < size_1; a++){
		S3[a] = S1[a];
	}
	S3[size_1] = 32;

	for (int b = (size_1 + 1); b < (size_1 + size_2); b++){
		S3[b] = S2[b];
	}
	S3[size_1 + size_2 + 1] = 0;
	

	cout << S3 << endl;

}


Hello i have been trying to do a function that unites strings entered by user,
i need to do this without any other libraries but i keep getting an output of the first word and dots. Please help, thanks :).
Suppose word1 = "hello" and word2 = "world"

1
2
3
for (int b = (size_1 + 1); b < (size_1 + size_2); b++){
	S3[b] = S2[b];
}


What will the initial value of b be? 6. So we get:
S3[6] = S2[6];
See how b isn't a valid index for S2 (I guess that is better said as not a valid index for the word in S2)?

You need a separate index for S3 and S2 (also, since you put a space between the two words you need to add one to the total length). Something like this should work:

1
2
3
for (int b = (size_1 + 1), c = 0; b < (size_1 + size_2 + 1); b++, c++){
	S3[b] = S2[c];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main ()
{
    string str[2] = {"one","two"};
    

    string x;
    
    x = str[0] + str[1];
    
    cout << x << endl;
    
    return 0;
}
Last edited on
closed account (48T7M4Gy)
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
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>

using namespace std;

int strlen(char* );
char* strcat(char*, char*);

const int MAXSIZE = 80;

int main()
{
    char* word1 = "trouble";
    char* word2 = "maker";

    char* w1w2 = strcat(word1, word2);
    cout << w1w2 << " is the new string.\n";
    cout << strlen( w1w2 ) << " is characters long.\n";

    char a[MAXSIZE];
    char b[MAXSIZE];

    cout << "Please enter 1st word: \n";
    cin >> a;
    cout << "Please enter 2nd word: \n";
    cin >> b;

    cout << strcat( a, b ) << " is the new string.";

    return 1;
}

int strlen(char* str)
{
    int i =0;
    while (str[i] != NULL)
        i++;

    return i;
}

char* strcat(char* s1, char* s2)
{
    int length1 = strlen(s1);
    int length2 = strlen(s2);

    int newSize = length1 + length2;

    int i, j;

    char* s3 = new char[newSize];

    for ( i = 0; i < length1 ; i++)
        s3[i] = s1[i];

    for ( j = i; j < newSize ; j++)
        s3[j] = s2[j-i];

    s3[newSize] = '\0';

    return s3;
}
Last edited on
char* s3 = new char[newSize + 1]; Do not forget to add space for null terminator, as right now s3[newSize] = '\0'; is UB. Remember, a[size] have only elements with indexes from 0 to size - 1!
closed account (48T7M4Gy)
My blooper was pretty fundamental.
Topic archived. No new replies allowed.