Advertisement
Guest User

Assignment 3 Segfault

a guest
May 12th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. #include<iostream>
  2. #include "mycstring.h"
  3.  
  4. using namespace std;
  5.  
  6. int main(){
  7.  
  8.     //
  9.     //  Declare a bunch of c strings for testing.
  10.     //
  11.  
  12.     char hello[] = "hello";
  13.     const char Hello[] = "Hello";
  14.     char *emptyHello;
  15.     const char intro[] = "My name is ___";
  16.     const char fun[] = "This assignment was challenging but fun.";
  17.     const char repeat[] = "Recursion recurses.";
  18.     const char repeat2[] = "Recursion recurses.";
  19.     const char programming[] ="Programming is telling the computer stuff.";
  20.     char computer[] = "I'm now telling the computer *exactly* what it can do with a lifetime supply of chocolate!";
  21.  
  22.     //
  23.     //  We need an instance to use the functions we wrote, since we
  24.     //  did put them inside of a class, after all
  25.     //
  26.  
  27.     // Use the default constructr, btw.
  28.     mycstring mystr;
  29.  
  30.     //  Print the first two strings, copy the first into the second,
  31.     //  print again to verify output.
  32.     //
  33.  
  34.     cout << endl << "Testing mystrcopy() - Copy one string into the other\n----------------\n\n";
  35.  
  36.     //Cant print an empty string - so simulate it - bad practice, but yea.
  37.     cout << "Empty Hello: " << endl << "Hello: " << Hello << endl;  
  38.  
  39.     emptyHello = mystr.mystrcopy(emptyHello, Hello);
  40.  
  41.    
  42.     cout << "Empty Hello: " << emptyHello << endl << "Hello: " << Hello << endl;  
  43.  
  44.     //
  45.     //  Test concatenation
  46.     //
  47.  
  48.     cout << endl << "Testing mystrcat() - Concatenates two strings\n----------------\n\n";
  49.  
  50.     cout << "Empty Hello: " << emptyHello << endl << " Hello:" << Hello << endl;  
  51.  
  52.     mystr.mystrcat(emptyHello, intro);
  53.  
  54.     cout << "Empty Hello: " << emptyHello << endl << " Hello:" << Hello << endl;  
  55.  
  56.  
  57.     //
  58.     //  Test comparison
  59.     // 
  60.  
  61.     cout << endl << "Testing mystrcmp() - Compare two strings\n----------------\n\n";
  62.    
  63.     int equal =  mystr.mystrcmp(repeat, repeat2);
  64.    
  65.     cout << "\"" << repeat << "\" and  \"" << repeat2 << "\" are " << (equal == 0 ? "" : "not") << " equal." << endl;
  66.    
  67.     equal = mystr.mystrcmp(repeat, hello);
  68.  
  69.     cout << "\"" << repeat << "\" and  \"" << hello << "\" are " << (equal == 0 ? "" : "not") << " equal." << endl;
  70.  
  71.     //
  72.     //  Check if a function contains a character
  73.     //  If it does, return a pointer to that character
  74.     //  Not quite sure how to test this one.
  75.     //
  76.     //  If we find the character, I look for the next one to print
  77.     //  by incrementing the pointer...
  78.     //
  79.  
  80.  
  81.     cout << endl << "Testing mystrchr() - Does a string contain a given character? \n----------------\n\n";
  82.    
  83.     char search = 'x';
  84.     char *result;
  85.    
  86.     result = mystr.mystrchr(computer, search);
  87.  
  88.     cout << "Searching inside of the string 'computer'. The string is defined as: " << computer << endl;
  89.     cout << "Result " << *result << " is in string computer" << endl;
  90.     cout << "The character after " << search << " is " << *(result+1) << endl;
  91.    
  92.     //
  93.     //  Test strlen
  94.     //
  95.  
  96.     cout << endl << "Testing mystrlen() - How long is a given string?? \n----------------\n\n";
  97.  
  98.     cout << "\"" << hello << "\"" << " is " << mystr.mystrlen(hello) << " chars long." << endl;
  99.  
  100.     cout << "\"" << computer<< "\"" << " is " << mystr.mystrlen(computer) << " chars long." << endl;
  101.  
  102.  
  103.     cout << endl << "All done!"  << endl << endl;
  104.  
  105.     //
  106.     //  return - Woot!
  107.     //
  108.  
  109.     return 0;
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement