Notes from 12/10

c-string.cpp

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

void testCompare(const char* s1, const char* s2);

int main()
{
    const char *s; /// pointer to char
    s = "Testing";  // String literals are not mutable
    // The const means that the array that s points to
    // cannot be modified.
    cout << s << "\n";
    // s itself can be modified to point to a different string
    s = "Different string";
    cout << s << "\n";

    // Can instead declare an array that's initialized with characters
    char m[100] = "This is a test";
    cout << m << "\n";
    m[4] = 'Z';
    cout << m << "\n";
    // Now the contents of m can be updated, but not m itself.
    //m = "Different string"; // error
    m[0] = 'D';
    m[1] = 'i';
    m[2] = 'f';
    m[3] = '\0';
    cout << m << "\n";

    // strlen   -- length up to but not including null.
    cout << strlen(m) << "\n"; // 3
    cout << strlen(s) << "\n"; // 16

    // String comparisons -- strcmp  returns an integer
    //   == 0  strings are equal
    //   < 0   first string comes first in alphabet
    //   > 0   first string comes second in alphabet
    testCompare("apple", "bee"); // -1
    testCompare("bee", "apple"); // 1
    testCompare("bee", "bee");   // 0
    testCompare("bee", "grape"); // -5 because 'b'-'g'
    testCompare("grape", "bee"); // +5 because 'g'-'b'
    testCompare("apple", "apply"); // -20 because 'e'-'y'
    testCompare("to", "too"); // -111, because '\0' - 'o'
    testCompare("Zebra", "apple"); // -7 because not case sensitive
    testCompare("Zebra", "Apple"); // +25 because 'Z'-'A'
    // There is NOT a version that is case-insensitive,
    // apparently I fantasized that.
    //cout << "stricmp(Zebra, apple) --> "
    //     << strcmpi("Zebra", "apple") << "\n";

    // Joining two strings together -- strcat
    char s1[100] = "Hello";
    char s2[] = "world";
    strcat(s1, s2); // append characters from s2 onto end of s1
    // BIG DANGEROUS CAVEAT: s1 must already be a big enough array
    // to store both strings.
    cout << s1 << "\n";

    return 0;


}

void testCompare(const char* s1, const char* s2)
{
    cout << "strcmp(" << s1 << ", " << s2 << ") --> "
         << strcmp(s1, s2) << "\n";
}

cpp-string.cpp

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()
{
    string s = "Hello";
    string t = "world";
    string k = s + t;
    cout << k << "\n";

    cout << k.size() << "\n"; // number of characters

    cout << k.find('o') << "\n"; // first o at position 4
    cout << k.find('o', 5) << "\n"; // next o is position 6
    if(k.find('z') == -1) {
        cout << "Did not find z\n";
    }

    cout << k.substr(3,5) << "\n";

    const char* p = k.c_str(); // convert to C string
    cout << strlen(p) << "\n";

    char q[] = "Testing this string";
    cout << string(q).substr(0,7) << "\n"; // convert to C++ string
    return 0;
}

struct.cpp

#include <iostream>

using namespace std;

struct card {     // declares a new type
    int rank;
    int suit;
};

// Arrays are a composite type where each element has the SAME type
// and you distinguish them by index.

// A struct is a composite type where each element type CAN be
// different, and you distinguish them by name.

void print_card(card c) {
    switch(c.rank) {
    case 1: cout << "Ace"; break;
    case 11: cout << "Jack"; break;
    case 12: cout << "Queen"; break;
    case 13: cout << "King"; break;
    default: cout << c.rank;
    }
    cout << " of ";
    switch(c.suit) {
    case 0: cout << "Spades"; break;
    case 1: cout << "Diamonds"; break;
    case 2: cout << "Hearts"; break;
    case 3: cout << "Clubs"; break;
    }
    cout << "\n";
}

int main()
{
    card deck[52];
    int i = 0;
    for(int s = 0; s < 4; s++) {
        for(int r = 1; r <= 13; r++) {
            deck[i].rank = r;
            deck[i].suit = s;
            print_card(deck[i]);
            i++;
        }
    }
}

list.cpp

#include <iostream>
using namespace std;

struct list {
    int data;
    list* next;
};

void print_list(list* p) {
    while(p != NULL) {
        cout << p->data << " ";
        p = p->next;
    }
}

int main()
{
    list* p = new list;
    p->data = 17;
    p->next = NULL;
    list* q = new list;
    q->data = 13;
    q->next = p;
    print_list(q);
    return 0;
}