//
// teststr.cpp
//
// Program to test the cstring class.
//
// Computer Science S-111, Harvard University
//

#include <iostream.h>
#include "cstring.hpp"


int
get_pos_int()
{
    int n;

    cin >> n;
    while (cin.fail() || n < 0) {
	cin.clear();
	cin.ignore(80, '\n');
	cout << "Not a valid value.  Please input a positive integer: ";
	cin >> n;
    }
    return n;
}

int
main()
{
    cstring *str1;
    int n;

    // Input, output, and length.
    cout <<  "Type a string:\n";
    cin >> str1;
    cout << "Your string is: " << str1 << endl;
    cout << "Its length is " << length(str1) << " characters.\n";

    // extract
    cout << "\nWhat # character to extract? ";
    n = get_pos_int();
    char ch = extract(str1, n);
    if (ch != 0) 
	cout << "That character is " << ch << endl;
    else
	cout << "The string is too short.\n";
    
    // index
    cout << "\nWhat character to search for? ";
    cin >> ch;
    n = index(str1 ,ch);
    if (n == 0)
	cout << "Not in the string.\n";
    else
	cout << "That is character # " << n << " in the string.\n";

    // split
    cstring *head, *tail;
    cout << "\nWhat # character to split after? ";
    n = get_pos_int();
    split(str1, n, head, tail);
    cout << "Head: " << head;
    cout << "     Tail: " << tail << endl;

    // cdelete and copystr
    cstring *copy;
    cout << "\nWhat # character to delete? ";
    n = get_pos_int();
    copy = copystr(str1);
    cdelete(str1, n);
    cout << str1 << endl;
    cout << "Unchanged copy: " << copy << endl;

    // insert
    cout << "\nWhat # character to insert before? ";
    n = get_pos_int();
    cout << "\nWhat character to insert? ";
    cin >> ch;
    cin.ignore(1);
    insert(str1, n, ch);
    cout << str1 << endl;

    // concat
    cstring *str2;
    cout << "\nType another string\n";
    cin >> str2;
    cout << "Its length is " << length(str2) << " characters.\n";
    cout << "Concatenation = ";
    cout << concat(str1, str2) << endl;

    // insert_sorted
    cstring *str3;
    cout << "\nType a string of characters in alphabetical order\n";
    cin >> str3;
    cout << "What character to insert in order? ";
    cin >> ch;
    cin.ignore(1);
    insert_sorted(str3, ch);
    cout << str3 << endl;
}    

