# C++: C-Strings - Programming Exercise Solutions - main

Here is a min program. The functions it calls are in another file, and an include file which contains those headers is used.

- C-Strings - Programming Exercise Solutions - functions
- C-Strings - Programming Exercise Solutions - header

      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
    // strings/cstring-array-main.cpp - Example C-string functions using arrays.
    // 2003-12-10  Fred Swartz

    //============================================ includes
    #include <iostream>
    #include <cstring>
    using namespace std;

    #include "cstring-array.h"

    //============================================ globals
    int testCount = 0;
    int testFailures = 0;

    //============================================ prototypes
    void check(bool passed, char testName[]);

    //============================================ main
    int main() {
        char s[100];
        char t[100];

        //-- trimRight
        strcpy(s, "How are you?   ");
        trimRight(s);
        check(strcmp(s, "How are you?")==0, "trimRight 1");

        strcpy(s, "How are you?");
        trimRight(s);
        check(strcmp(s, "How are you?")==0, "trimRight 2");

        strcpy(s, "");
        trimRight(s);
        check(strcmp(s, "")==0, "trimRight 3");

        //-- truncate
        strcpy(s, "How are you?");
        truncate(s, 3);
        check(strcmp(s, "How")==0, "truncate 1");

        //-- padRight
        strcpy(s, "test");
        padRight(s, 6);
        check(strcmp(s, "test  ")==0, "padRight 1");

        strcpy(s, "test");
        padRight(s, 2);
        check(strcmp(s, "test")==0, "padRight 2");

        //-- isAlpha
        check( isAlpha("test"), "isAlpha 1");
        check( isAlpha("")    , "isAlpha 2");
        check(!isAlpha("a ")  , "isAlpha 3");
        check(!isAlpha("123") , "isAlpha 4");

        //-- count
        check(count("abracadabra", 'a')==5, "count 1");
        check(count("xyz", 'a') == 0      , "count 2");
        check(count("", 'a') == 0         , "count 3");

        cout << endl << "Summary: Passed " << testCount-testFailures
                     << ", Failed " << testFailures << endl;

        char x; cout << "Enter any char to exit."; cin.get(x); // keep window open
        return 0;
    }

    //============================================ check
    void check(bool passed, char testName[]) {
        testCount++;
        if (passed) {
            cout << "Passed " << testName << endl;
        } else {
            cout << "FAILED " << testName << endl;
            testFailures++;
        }
    }