Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include "mycstring.h"
- using namespace std;
- int main(){
- //
- // Declare a bunch of c strings for testing.
- //
- char hello[] = "hello";
- const char Hello[] = "Hello";
- char *emptyHello;
- const char intro[] = "My name is ___";
- const char fun[] = "This assignment was challenging but fun.";
- const char repeat[] = "Recursion recurses.";
- const char repeat2[] = "Recursion recurses.";
- const char programming[] ="Programming is telling the computer stuff.";
- char computer[] = "I'm now telling the computer *exactly* what it can do with a lifetime supply of chocolate!";
- //
- // We need an instance to use the functions we wrote, since we
- // did put them inside of a class, after all
- //
- // Use the default constructr, btw.
- mycstring mystr;
- // Print the first two strings, copy the first into the second,
- // print again to verify output.
- //
- cout << endl << "Testing mystrcopy() - Copy one string into the other\n----------------\n\n";
- //Cant print an empty string - so simulate it - bad practice, but yea.
- cout << "Empty Hello: " << endl << "Hello: " << Hello << endl;
- emptyHello = mystr.mystrcopy(emptyHello, Hello);
- cout << "Empty Hello: " << emptyHello << endl << "Hello: " << Hello << endl;
- //
- // Test concatenation
- //
- cout << endl << "Testing mystrcat() - Concatenates two strings\n----------------\n\n";
- cout << "Empty Hello: " << emptyHello << endl << " Hello:" << Hello << endl;
- mystr.mystrcat(emptyHello, intro);
- cout << "Empty Hello: " << emptyHello << endl << " Hello:" << Hello << endl;
- //
- // Test comparison
- //
- cout << endl << "Testing mystrcmp() - Compare two strings\n----------------\n\n";
- int equal = mystr.mystrcmp(repeat, repeat2);
- cout << "\"" << repeat << "\" and \"" << repeat2 << "\" are " << (equal == 0 ? "" : "not") << " equal." << endl;
- equal = mystr.mystrcmp(repeat, hello);
- cout << "\"" << repeat << "\" and \"" << hello << "\" are " << (equal == 0 ? "" : "not") << " equal." << endl;
- //
- // Check if a function contains a character
- // If it does, return a pointer to that character
- // Not quite sure how to test this one.
- //
- // If we find the character, I look for the next one to print
- // by incrementing the pointer...
- //
- cout << endl << "Testing mystrchr() - Does a string contain a given character? \n----------------\n\n";
- char search = 'x';
- char *result;
- result = mystr.mystrchr(computer, search);
- cout << "Searching inside of the string 'computer'. The string is defined as: " << computer << endl;
- cout << "Result " << *result << " is in string computer" << endl;
- cout << "The character after " << search << " is " << *(result+1) << endl;
- //
- // Test strlen
- //
- cout << endl << "Testing mystrlen() - How long is a given string?? \n----------------\n\n";
- cout << "\"" << hello << "\"" << " is " << mystr.mystrlen(hello) << " chars long." << endl;
- cout << "\"" << computer<< "\"" << " is " << mystr.mystrlen(computer) << " chars long." << endl;
- cout << endl << "All done!" << endl << endl;
- //
- // return - Woot!
- //
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement