Solutions to Assignment 4


// 4.1

#include 
// using namespace std;

/*
 * Requested function
 */
char *
my_strstr(char *ps1, char *ps2) {
    char* pss2 = ps2, *psss1 = ps1, *pss1 = ps1;

    if (!ps1 || !ps2) return NULL;
    while (*pss1) {
       if (*pss1 == *pss2){
           for (psss1 = pss1; *psss1 == *pss2; psss1++, pss2++);
           if (!*pss2) return pss1;
       } else 
           pss2 = ps2;
       pss1++;
    }
    return NULL;
}


int main () {
    char s1[500];
    char s2[500];

    cout << "Enter the two strings:\n";
    cin >> s1 >> s2;
    cout << my_strstr(s1, s2) << "\n";
    return 1;

}


    [ Part 3: "Attached Text" ]


#include 
// use namespace std;

int
main(int argc, char **argv) {
    
    if (argc < 2) {
        cout << "Incorrect call. Please provide an email address\n";
        return -1;
    }
    char *email = argv[1];
    int status = 1, chars = 0, at = 0, points = 0;
    while (*email) {
       switch (*email) {
           case '@':
               at++;
               if ((at > 1) || !chars) status = 0;
               chars = 0;
               break;
           case '.':
               points++;
               if (!at || !chars) status = 0;
               chars = 0;
               break;
           case ' ':
               status = 0;
           default:
               chars++;
               break;
       }
       email++;
    }
    if (!points || !chars) status = 0;
    cout << "Address '" << argv[1] << "' is " 
         << (status ? "valid." : "not valid.") << endl;
    return status;
}


    [ Part 4: "Attached Text" ]


#include 
//use namspace std;

#define MIN(X,Y) ((X) > (Y) ? (Y) : (X))

int
main(int argc, char **argv) {
    int C[100][100];
    int R, N;

    cout << "Enter N and R: ";
    cin >> N >> R;
    // Init stage of recursive process
    for (int i = 0; i < N; i++) 
        for (int j = 0; j < R; j++)
            C[i][j] = -1;
    for (int i = 0; i < N; i++) 
        C[i][0] = 1;
    for (int i = 0; i < MIN(R, N); i++) 
        C[i][i] = 1;

    cout << "Before:" << endl;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < R; j++) 
            cout << C[i][j] << "\t";
        cout << endl;
    }

    // Computation stage
    for (int i = 1; i < N; i++)
        for (int j = 1; j < MIN(R, i); j++)
            C[i][j] = C[i-1][j-1] + C[i-1][j];

    cout << "After: " << endl;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < R; j++)
            cout << C[i][j] << "\t";
        cout << endl;
    }


    return 1;

}


    [ Part 5: "Attached Text" ]

#include 
#include 
#include 
// using namespace std;

/* Comment 1 - style C */

int 
main (int argc, char **argv) {
    char a, b;
    ifstream infile;
    ofstream outfile;
   

    if (argc < 3) {
        cout << "Please provide input/output files\n";
        return -1;
    } // Comment 2 - style C++ 
    infile.open(argv[1]);
    outfile.open(argv[2]);
    if (!infile || !outfile) {
        cout << "Unable to open some files\n";
        return -1;
    }

    infile >> resetiosflags(ios::skipws);
    while (infile >> a) {
       if (a == '/'){
           b = a;
           infile >> a;    
           if ((a != '/') && (a != '*')) {
                outfile << b;
           } else if (a == '/') {
                while (infile >> a) { 
                    if (a == '\n') break;
                }
           } else if (a == '*'){
                while (infile >> a) {
                     if (a == '*') {
                         infile >> a;
                         if (a == '/') {
                            infile >> a;
                            break;		/* Comment 3 */
                         }
                     }
                }
           }       
        }
        outfile << a;
    }
    return 0;
}

    [ Part 6: "Attached Text" ]


#include 
#include 
#include 
// use namespace std;

int 
f(int *a, int N) {
    int val = 1;

    for (int i = 0; i < N; i++) 
        if (i+1 != a[i]) 
            val = 0;
    return val;
}

int
main () {
    int buf[100], N = 100;

    srand(time(NULL));
    for (int i = 0; i < N; i++)
        buf[i] = rand() % (i+1);
    cout << "Result is: " << (f(buf, N) ? "true." : "false.") << endl;
    return 1;
}