How would you strcat() all strings inside a 2D array?

closed account (yqD8vCM9)
I have a 2D array with 20 strings. I'm trying to strcat these strings so that it becomes 10 strings. But I'm having problems. It isn't working correctly. myStrCat is equivalent to strcat(). I just made my out.
[code]
Last edited on
closed account (28poGNh0)
I am not sure what are you trying to do !!!!!!!!!!!!!!! you need to explain more

I think you want something like this

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
# include <iostream>
# include <cstring>
using namespace std;

int main()
{
    char _2DString[20][20] = {"apple"," is good",
                               "annas"," is bad",
                               "shoes"," is black",
                               "two"," is a number",
                               "tie"," is tall",
                               "car"," is red",
                               "man"," loves women",
                               "women","loves money",
                               "money"," loves to be spent",
                               "my vocabilary"," is suck"};
    char string[10][20];

    for(int i=0,j=0;i<20;i+=2)
    {
        strcat(_2DString[i],_2DString[i+1]);
        strcpy(string[j++],_2DString[i]);
    }

    for(int i=0;i<10;i++)
    {
        cout << string[i] << endl;
    }

    return 0;
}


or something like this

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
# include <iostream>
# include <cstring>
using namespace std;

int main()
{
    char _2DString[20][2][20] = {{"apple"," is good"},
                               {"annas"," is bad"},
                               {"shoes"," is black"},
                               {"two"," is a number"},
                               {"tie"," is tall"},
                               {"car"," is red"},
                               {"man"," loves women"},
                               {"women","loves money"},
                               {"money"," loves to be spent"},
                               {"my vocabilary"," is suck"},
                               {"my vocabilary"," is suck"},
                               {"my vocabilary"," is suck"},
                               {"my vocabilary"," is suck"},
                               {"my vocabilary"," is suck"},
                               {"my vocabilary"," is suck"},
                               {"my vocabilary"," is suck"},
                               {"my vocabilary"," is suck"},
                               {"my vocabilary"," is suck"},
                               {"my vocabilary"," is suck"},
                               {"my vocabilary"," is suck"}};

    for(int i=0;i<20;i++)
    {
        strcat(_2DString[i][0],_2DString[i][1]);
    }

    for(int i=0;i<20;i++)
    {
        cout << _2DString[i][0] << endl;
    }

    return 0;
}


I hope you dont ask for a fourth exp
Last edited on
Topic archived. No new replies allowed.