String operations

These are notes for 13 November.

String operations

We discussed two operations on the string type. Recall that char is the type of single characters, which are represented in single quotes. Strings are sequences of characters, represented in double quotes:

char c = 'A';
string s = "This is a test";

The first operation we learned was s.length(). Given a string variable s, this returns the number of characters in that string.

cout << s.length() << "\n";  // outputs 14
string k = "Hello";
cout << k.length() << "\n"; // outputs 5
string g = "";
cout << g.length() << "\n"; // outputs 0

The other operation we learned was s[i] where i is some integer variable or expression. This returns the character at that position. We start counting positions from zero, so:

string z = "Alpha";
cout << z[0] << "\n";  // outputs just 'A'
cout << z[2] << "\n";  // outputs just 'p'

The next section demonstrates a fragment of code that prints every other character in the string (note the i += 2 in the loop). It’s output will be rdconcr.

everyother.cpp

#include <iostream>
using namespace std;
int main()
{
    // Demonstrate the length and bracket operations on strings.
    string message = "red clown car";
    for(int i = 0; i < message.length(); i += 2)
    {
        cout << message[i];
    }
    cout << "\n";
    return 0;
}

a8sol.cpp

#include <iostream>
#include <iomanip>
using namespace std;

int ask_user_for_year();
void print_month(int month);
void print_header(int month, int year);
int compute_month_start(int month, int year);
int ask_user_for_month();
void print_chars(int k, char c);
int lookup_days_in_month(int m, int y);
void print_calendar_grid(int dow, int max);

int main()
{
    int year = ask_user_for_year();
    int month = ask_user_for_month();
    int dayOfWeek = compute_month_start(month, year);
    int daysInMonth = lookup_days_in_month(month, year);
    print_header(month, year);
    print_calendar_grid(dayOfWeek, daysInMonth);
   return 0;
}

int ask_user_for_year()
{
    int year;
    while(1)
    {
        cout << "Enter year (1900-): ";
        cin >> year;
        if(year >= 1900)
        {
            return year;
        }
        cout << "ERROR, out of range.\n";
    }
}

void print_calendar_grid(int dow, int max)
{
   print_chars(4*dow, ' ');
   int day = 1;
   while(day <= max)
   {
        cout << setw(4) << day;
        day++;
        if(dow == 6) // Saturday
        {
            cout << "\n";
        }
        dow = (dow + 1) % 7;
   } 
}

int lookup_days_in_month(int month, int year)
{
    int daysInMonth = 31;
    // TODO: leap year
    if(month == 2) { daysInMonth = 29; }
    else if(month == 4 || month == 6 || month == 9
            || month == 11)
    { daysInMonth = 30; }
    return daysInMonth;
}

void print_chars(int k, char c)
{
    for(int i = 0; i < k; i++)
    {
        cout << c;
    }
}


int ask_user_for_month()
{
    int month;
    cout << "Enter month (1-12): ";
    cin >> month;
    while(month < 1 || month > 12)
    {
        cout << "ERROR. Enter month (1-12): ";
        cin >> month;
    } 
    return month;
}

void print_month(int month)
{
    switch(month)
    {
    case 1: cout << "January"; break;
    case 2: cout << "February"; break;
    case 3: cout << "March"; break;
    case 4: cout << "April"; break;
    case 5: cout << "May"; break;
    case 6: cout << "June"; break;
    case 7: cout << "July"; break;
    case 8: cout << "August"; break;
    case 9: cout << "September"; break;
    case 10: cout << "October"; break;
    case 11: cout << "November"; break;
    case 12: cout << "December"; break;
   }
}

void print_header(int month, int year)
{
    cout << "\n";
    print_month(month);   
    cout << " " << year << "\n\n";
    cout << " Sun Mon Tue Wed Thu Fri Sat\n";
}

int compute_month_start(int month, int year)
{
   // on what day does the month start?
    static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    int y = year - (month < 3);
    int dayOfWeek = (y + y/4 - y/100 + y/400 + t[month-1] + 1) % 7;
    return dayOfWeek;
}