post
poster: Comrade-Sergei
description: Concatenation
language: C++
[download]
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
// Concatenate - concatenate two strings with a "-" in the middle

#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <strings.h>
// yes I know, deprecated, get over it...
using namespace std;

int main(int argc, char *argv[])
{
    // Reads the first string
    char szString1[256];
    cout << "Enter string 1:";
    cin >> szString1;
    
    // safer alternative... 
    //cin.getline(szString1, 128);
    
    // Now the second string...
    char szString2[128];
    cout << "Enter string 2:";
    cin >> szString2;

    //Again safer alt. 
    // cin.getline(szString2, 128);
    
    //Accumulate both strings into a single buffer...
    
    char szString[260];
    
    //Copt the first stinf into the buffer
    strncpy(szString, szString1, 128);
    
    // ...and concatenate a " - " on to the first
    strncat(szString, " - ", 4);
    
    //...now add the second string to the mix
    strncat(szString, szString2, 128);
    
    //... and display the result
    cout << "\n" << szString << endl;
    
    system("PAUSE");
    return 0;
}