Skip to main content Link Menu Expand (external link) Document Search Copy Copied

CS162 - 2A

TABLE OF CONTENTS

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <cstring>

using namespace std;

void checkSubstring(char s1[], char s2[], int n, int m);
void concatenate(char* s1, char* s2 char*& str);

int main() {
    char * stmp = new char[100000];
    cout << "Please type in the first string: ";
    cin.get(stmp, 100000, '\n');
    cin.ignore();
    
    int n = int(strlen(stmp));
    char *str1 = new char[n + 1];
    for (int i=0; i <= n; ++i) {
        str1[i] = stmp[i];
    }
    
    cout << "Please type in the second string: ";
    cin.get(stmp, 100000, '\n');
    cin.ignore();
    
    int m = int(strlen(stmp));
    char *str2 = new char[m + 1];
    for (int i=0; i <= m; ++i) {
        str2[i] = stmp[i];
    }
    
    delete[] stmp;
    
    checkSubstring(str1, str2, n, m);
    
    char* str;
    
    cout << '\n' << "The merged string is: ";
    concatenate(str1, str2, str);
    for (int i=0; i<m+n; i++) {
        cout<<str[i];
    }
    str[n+m] = '\0';
    cout << '\n';
    
    delete[] str1;
    delete[] str2;
    delete[] str;
    return 0;
}

void checkSubstring(char s1[], char s2[], int n, int m){
    for (int i = 0; i < m-n; ++i) {
        if (s2[i] == s1[0]) {
            int j;
            for(j = 1; j < n; ++j) {
                if(s1[j] != s2[i+j])
                {
                    break;
                }
            }
            if (j==n) {
                cout << "s1 is a substring of s2";
                return;
            }
        }
    }
    cout << "s1 is not a substring of s2";
}

void concatenate(char* s1, char* s2 char*& str) {
    int n = strlen(str1);
    int m = strlen(str2);
    str = new char[n+m+1];
    for (int i = 0; i < n; ++i) {
        str[i] = s1[i];
    }
    for (int i = 0; i <= m; ++i) {
        str[i + n] = s2[i];
    }
}