// C-String functions example

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

void printStringBytes(const char* str, int bytes);

int main()
{
    // strcpy & strncpy
    char array1[32];
    char array2[8];
    // array1 = "have a nice day";     // Why is this a compile error?
    strcpy(array1,"have a nice day");
    cout << __LINE__ << ": " << array1 << endl;
    int x = 9;
    cout << __LINE__ << ": " << x << endl;
    strcpy(array2,"have a nice day");
    cout << __LINE__ << ": " << array2 << endl;
    cout << __LINE__ << ": " << x << endl;                // Why?
    strncpy(array2,"Wow",3);
    cout << __LINE__ << ": " << array2 << endl;           // Why?
    strncpy(array2,"Wow",4);
    cout << __LINE__ << ": " << array2 << endl;

    // strcat & strncat
    strcpy(array1,"one");
    strcat(array1,"two");
    cout << __LINE__ << ": " << array1 << endl;
    strncat(array1,"three",3);
    cout << __LINE__ << ": " << array1 << endl;
    strncat(array1,"four",3);
    cout << __LINE__ << ": " << array1 << endl;
    strncat(array1,"five",5);
    cout << __LINE__ << ": " << array1 << endl;
    strcat(array1,"six");
    cout << __LINE__ << ": " << array1 << endl;

    // strcmp & strncmp
    cout << __LINE__ << ": " << strcmp("less","greater") << endl;
    cout << __LINE__ << ": " << strcmp("greater","less") << endl;
    cout << __LINE__ << ": " << strcmp("equal","equal") << endl;
    cout << __LINE__ << ": " << strcmp("something","some") << endl;
    cout << __LINE__ << ": " << strncmp("something","some",4) << endl;

    // strstr
    char* ptr2char;
    const char* ptr2constchar;
    ptr2constchar = strstr("have a nice day","nice");
    cout << "ptr2constchar=" << ptr2constchar << endl;
    ptr2constchar = strstr("have a nice day","very nice");
    cout << "ptr2constchar=" << ptr2constchar << endl;
    cout << "Oh no! cout is broken" << endl;
    cout.clear();
    cout << "Whew! cout is fixed again!" << endl;
    strcpy(array1,"have a nice day");
    ptr2char = strstr(array1,"nice");
    *ptr2char = '$';
    cout << __LINE__ << ": " << ptr2char << endl;

    // strchr & strrchr
    cout << __LINE__ << ": " << strchr("have a nice day",'a') << endl;
    cout << __LINE__ << ": " << strrchr("have a nice day",'a') << endl;

    // strlen
    cout << __LINE__ << ": " << strlen("have a nice day") << endl;

    // strtok
    char array3[] = "HAVE A NICE DAY";
    printStringBytes(array3,strlen(array3)+1);
    ptr2char = strtok(array3,"ABC");
    cout << __LINE__ << ": " << ptr2char << endl;
    printStringBytes(array3,sizeof(array3));
    ptr2char = strtok(NULL,"ABC");
    cout << __LINE__ << ": " << ptr2char << endl;
    printStringBytes(array3,sizeof(array3));
    while (ptr2char != NULL)
    {
        ptr2char = strtok(NULL,"ABC");
        cout << ptr2char << endl;
        printStringBytes(array3,sizeof(array3));
    }
}

void printStringBytes(const char* str, int bytes)
{
    for (int i = 0; i < bytes; i++)
        cout << static_cast<int>(*(str+i)) << ' ';
    cout << endl;
}