Lecture 16 Exercise

(1)What is the output of the following programs?
(a)
#include<iostream>
#include<cstring>
using namespace std;
    
int main() {
    char a[15] = "ABCDE";
    char b[15];
    char c[15]= "123456789";
    char d[] = "abcde";
    strcpy(b, a);
    cout << "1: " << a << endl;
    cout << "2: " << b << endl;
    strncpy(b, c, 5);
    cout << "3: " << b << endl;
    cout << "4: " << c << endl;
    strncpy(c,a,2);
    cout << "5: " << a << endl;
    cout << "6: " << c << endl;
    strcpy(c,d);
    cout << "7: " << c << endl;
    cout << "8: " << d << endl;

    return 0;
}
(b)
#include<iostream>
#include<cstring>
using namespace std;
    
int main() {
    char a[20] = "12345";
    char b[20] = "ijklm";
    char c[20] = "Hello!!!";

    cout << "1: " << a << endl;
    b[2] = '#';
    cout << "2: " << b << endl;
    cout << "3: " << c << endl;
    strcat(a,b);
    cout << "4: " << a << endl;
    cout << "5: " << b << endl;

    strncat(c, b, 3);
    cout << "6: " << c << endl;
    cout << "7: " << b << endl;

    return 0;
}
(c)
#include<iostream>
#include<cstring>
using namespace std;

void check(int a);

int main() {
    char a[10] = "apple", b[10] = "apply";
    char c[10] = "banana", d[10] = "ban";
    char e[10] = "computer", f[10] = "camp";
    check(strcmp(a,b));
    check(strcmp(b,c));
    check(strcmp(a,a));
    check(strncmp(a,b,3));
    check(strcmp(e,f));
    check(strncmp(f,e,2));
    strcpy(b,d);
    check(strcmp(b,d));

    check(strcmp(f, "CAMP"));
    check(strcmp("paste", "copy"));
    check(strcmp("hello", "hello"));
    return 0;
}

void check(int a) {
    if( a == 0) {
        cout << "0\n";
    } else if (a > 0) {
        cout << "+\n";
    } else {
        cout << "-\n";
    }
}

(d) What is the output of the following if I input 2 "Charles Li"
#include<iostream>
#include<cstring>
using namespace std;


int main() {
    char name1[20], name2[20];
    cout << "Input name1: ";
    cin.getline(name1, 20);
    
    cout << "Input name2: ";
    cin >> name2;

    cout << "Hello " << name1 << endl;
    cout << "Hello " << name2 << endl;

    return 0;
}

(e)
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;


int main() {
    char a[] = {'a', '2', '#', 'Q'};
    for(int i = 0; i < 4; i++){
        cout << a[i] << endl;

        if(islower(a[i])) {
            cout << "lower ";
        }

        if(isupper(a[i])) {
            cout << "upper ";
        }

        if(isdigit(a[i])) {
            cout << "digit ";
        }

       cout << (char)toupper(a[i])<< " ";
       cout << (char)tolower(a[i])<< " ";
       cout << "\n\n";
    }
}
(2)
(a)
Write a function void switchCase(char string[]). The function replaces all the uppercase letters (resp. lowercase letters) to uppercase letters (resp. uppercase letters).
Example:
char a[] = "aBcD123eF"; then after the function call switchCase(a), cout << a becomes "AbCd123Ef"
(b)
Write a function void countDigit(char string[]) counts the number of digits in the CString string.
(c)
Write a function void removeDigit(char string[], char newstring[]) removes all the digits from string and save the result to newstring.
Example:
char a[] = "aBcD123eF", b[100]; then after removeDigit(a,b), b becomes "aBcDeF".