Advertisement
Garey

Dobcho_String_Reimplementation

Apr 24th, 2018
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.85 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. using namespace std;
  4.  
  5. /*! @class String <iostream>
  6. *  @brief A custom String implementation
  7. *
  8. *  A custom C++ implementation of a String object for pedagogical purposes.
  9. *  Is efficient for specifying the length of your String and does not depend
  10. *  on the use of a NULL termination.
  11. *
  12. *  @author Dobromir Stoyanov
  13. *  @version 0.2
  14. *  @date 2018-04-25
  15. *  @invariant String data will always point to NULL or a valid address.
  16. *  @invariant String length will always indicate amount of allocated space.
  17. */
  18. class String
  19. {
  20.     char*    data;    /*!< The ASCII characters that comprise the String */
  21.     unsigned length;  /*!< The number of characters allocated in data */
  22.  
  23. public:
  24.  
  25.     /*!
  26.     *  @brief Empty String Constructor.
  27.     *  @post String length is set equal to 0.
  28.     */
  29.     String();
  30.  
  31.     /*!
  32.     *  @brief Single-character String Constructor.
  33.     *  @param[in] c A char literal.
  34.     *  @post String data equals @a c and String length equals 1.
  35.     */
  36.     String(char c);
  37.  
  38.     /*!
  39.     *  @brief char* String Constructor
  40.     *  @param[in] c A char* null-terminated string.
  41.     *  @post String length equals @code{.cpp}strlen(c)@endcode.
  42.     *  @post String data equals @a c allocated values, except the null-terminator.
  43.     */
  44.     String(const char* c);
  45.  
  46.     /*!
  47.     *  @brief Copy String Constructor
  48.     *  @param[in] s A String instance.
  49.     *  @post This String will be identical to String @a s.
  50.     */
  51.     String(const String& s);
  52.  
  53.     /*!
  54.     *  @brief Default String Destructor
  55.     *  @post String data is deleted.
  56.     */
  57.     ~String();
  58.  
  59.     /*!
  60.     *  @brief String length.
  61.     *  @return Value in String @c length.
  62.     */
  63.     unsigned len() const;
  64.  
  65.     /*!
  66.     *  @brief String index of @a c.
  67.     *  @param[in] c A char literal.
  68.     *  @return The index of @a c in String, if exists; -1 otherwise.
  69.     */
  70.     int index(char c) const;
  71.  
  72.     /*!
  73.     *  @brief Converts lowercase alphabetic characters to uppercase.
  74.     *  @param[in] first Starting index of String to change case.
  75.     *  @param[in] last  Ending index of String to change case.
  76.     *  @pre @a first is less than or equal to @a last and @a last is less than
  77.     *       or equal to String length.
  78.     *  @post All lowercase alphabetic characters in String data greater than
  79.     *        or equal to @a first, but less than @a last are uppercase.
  80.     *  @throw int
  81.     */
  82.     void upcase(unsigned first, unsigned last);
  83.  
  84.     /*!
  85.     *  @brief Converts uppercase alphabetic characters to lowercase.
  86.     *  @param[in] first Starting index of String to change case.
  87.     *  @param[in] last Ending index of String to change case.
  88.     *  @pre @a first is less than or equal to @a last and @a last is less than
  89.     *       or equal to String length.
  90.     *  @post All uppercase alphabetic characters in String data greater than
  91.     *        or equal to @a first, but less than @a last are lowercase.
  92.     *  @throw int
  93.     */
  94.     void downcase(unsigned first, unsigned last);
  95.  
  96.     /*!
  97.     *  @brief Toggles lowercase alphabetic characters to uppercase, and vice versa.
  98.     *  @param[in] first Starting index of String to change case.
  99.     *  @param[in] last  Ending index of String to change case.
  100.     *  @pre @a first is less than or equal to @a last and @a last is less than
  101.     *       or equal to String length.
  102.     *  @post All lowercase alphabetic characters in String data greater than
  103.     *        or equal to @a first, but less than @a last are uppercase, and
  104.     all uppercase alphabetic characters in String data are lowercase.
  105.     *  @throw int
  106.     */
  107.     void togglecase(unsigned first, unsigned last);
  108.  
  109.     //@{
  110.     /*!
  111.     *  @brief Stream operators.
  112.     *  @param so A stream object.
  113.     *  @param s  A String object.
  114.  
  115.     *  @return Stream object containing String content.
  116.     */
  117.     friend std::ostream& operator<< (std::ostream& so, const String& s);
  118.     friend std::istream& operator>> (std::istream& so, String& s);
  119.     //@}
  120.  
  121.     //@{
  122.     /*!
  123.  
  124.     *  @brief Access String character.
  125.     *  @param j Index value in String.
  126.     *  @pre @a j is less than String length.
  127.     *  @return character at @a j index of String data.
  128.     *  @throw int
  129.  
  130.     */
  131.     char  operator[] (unsigned j) const;
  132.     char& operator[] (unsigned j);
  133.     //@}
  134.  
  135.     /*!
  136.     *  @brief Sets String value.
  137.     *  @param[in] s A String object.
  138.     *  @return A String reference to *this.
  139.     *  @post String will be equivalent to @a s.
  140.     */
  141.     String& operator= (const String& s);
  142.  
  143.     /*!
  144.     *  @brief Append to String.
  145.     *  @param[in] s A String object.
  146.     *  @return A String reference to *this.
  147.     *  @post String will equal the concatenation of itself with @a s.
  148.     */
  149.     String& operator+= (const String& s);
  150.  
  151.     //@{
  152.     /*!
  153.     *  @brief String concatenation (addition).
  154.     *  @param[in] lhs The left-hand operand String or String convertable.
  155.     *  @param[in] rhs The right-hand operand String or String convertable.
  156.     *  @return Copy of a String object.
  157.     *  @post The String will be the concatenation of @a lhs and @a rhs.
  158.     */
  159.     friend String operator+ (const String& lhs, const String& rhs);
  160.     friend String operator+ (const String& lhs, char          rhs);
  161.     friend String operator+ (const String& lhs, const char*   rhs);
  162.     friend String operator+ (char          lhs, const String& rhs);
  163.     friend String operator+ (const char*   lhs, const String& rhs);
  164.     //@}
  165.  
  166.     //@{
  167.     /*!
  168.     *  @brief String equality
  169.     *  @param[in] lhs The left-hand operand String or String convertable.
  170.     *  @param[in] rhs The right-hand operand String or String convertable.
  171.     *  @return True, if @a lhs and @a rhs have the same length, and each
  172.     *          character in their String data are identical in both value
  173.     *          and index.
  174.     */
  175.     friend bool operator== (const String& lhs, const String& rhs);
  176.     friend bool operator== (const String& lhs, char          rhs);
  177.     friend bool operator== (const String& lhs, const char*   rhs);
  178.     friend bool operator== (char          lhs, const String& rhs);
  179.     friend bool operator== (const char*   lhs, const String& rhs);
  180.     //@}
  181.  
  182.     //@{
  183.     /*!
  184.     *  @brief String inequality: Greater-than.
  185.     *  @param[in] lhs The left-hand operand String or String convertable.
  186.     *  @param[in] rhs The right-hand operand String or String convertable.
  187.     *  @return True, if @a lhs is in dictionary order (Capitals-first) to
  188.     *          @a rhs when comparing alphabetical characters or @a lhs is
  189.     *          greater in ASCII value to @a rhs, in corresponding String
  190.     *          data indices.
  191.     */
  192.     friend bool operator> (const String& lhs, const String& rhs);
  193.     friend bool operator> (const String& lhs, char          rhs);
  194.     friend bool operator> (const String& lhs, const char*   rhs);
  195.     friend bool operator> (char          lhs, const String& rhs);
  196.     friend bool operator> (const char*   lhs, const String& rhs);
  197.     //@}
  198.  
  199.     //@{
  200.     /*!
  201.  
  202.     *  @brief String non-equality
  203.     *  @param[in] lhs The left-hand operand String or String convertable.
  204.     *  @param[in] rhs The right-hand operand String or String convertable.
  205.     *  @return True, if @a lhs is not equal to @a rhs.
  206.     *  @see String::operator==
  207.  
  208.     */
  209.     friend bool operator!= (const String& lhs, const String& rhs);
  210.     friend bool operator!= (const String& lhs, char          rhs);
  211.     friend bool operator!= (const String& lhs, const char*   rhs);
  212.     friend bool operator!= (char          lhs, const String& rhs);
  213.     friend bool operator!= (const char*   lhs, const String& rhs);
  214.     //@}
  215.  
  216.     //@{
  217.     /*!
  218.     *  @brief String inequality: Less-than.
  219.     *  @param[in] lhs The left-hand operand String or String convertable.
  220.     *  @param[in] rhs The right-hand operand String or String convertable.
  221.     *  @return True, if @a lhs is neither equal to, nor greater-than @a rhs.
  222.     *  @see String::operator==,String::operator>
  223.     */
  224.     friend bool operator< (const String& lhs, const String& rhs);
  225.     friend bool operator< (const String& lhs, char          rhs);
  226.     friend bool operator< (const String& lhs, const char*   rhs);
  227.     friend bool operator< (char          lhs, const String& rhs);
  228.     friend bool operator< (const char*   lhs, const String& rhs);
  229.     //@}
  230.  
  231.     //@{
  232.     /*!
  233.     *  @brief String inequality: Less-than or equal
  234.     *  @param[in] lhs The left-hand operand String or String convertable.
  235.     *  @param[in] rhs The right-hand operand String or String convertable.
  236.     *  @return True, if @a lhs is not greater-than @a rhs.
  237.     *  @see String::operator>
  238.     */
  239.     friend bool operator<= (const String& lhs, const String& rhs);
  240.     friend bool operator<= (const String& lhs, char          rhs);
  241.     friend bool operator<= (const String& lhs, const char*   rhs);
  242.     friend bool operator<= (char          lhs, const String& rhs);
  243.     friend bool operator<= (const char*   lhs, const String& rhs);
  244.     //@}
  245.  
  246.     //@{
  247.     /*!
  248.     *  @brief String inequality: Greater-than or equal
  249.     *  @param[in] lhs The left-hand operand String or String convertable.
  250.     *  @param[in] rhs The right-hand operand String or String convertable.
  251.     *  @return True, if @a lhs is greater-than or equal to @a rhs.
  252.     *  @see String::operator>,String::operator==
  253.     */
  254.     friend bool operator>= (const String& lhs, const String& rhs);
  255.     friend bool operator>= (const String& lhs, char          rhs);
  256.     friend bool operator>= (const String& lhs, const char*   rhs);
  257.     friend bool operator>= (char          lhs, const String& rhs);
  258.     friend bool operator>= (const char*   lhs, const String& rhs);
  259.     //@}
  260. };
  261.  
  262.  
  263. String::String()
  264. {
  265.     length = 0;
  266.     data = new char[0];
  267. }
  268.  
  269. String::String(char c)
  270. {
  271.     length = 1;
  272.     data = new char(c);
  273. }
  274.  
  275. String::String(const char* c)
  276. {
  277.     if (c)
  278.     {
  279.         unsigned n = 0;
  280.         while (c[n] != '\0') n++;
  281.         length = n;
  282.         data = new char[n];
  283.         for (unsigned j = 0; j < n; j++)
  284.             data[j] = c[j];
  285.     }
  286.     else
  287.     {
  288.         length = 0;
  289.         data = new char[0];
  290.     }
  291. }
  292.  
  293. String::String(const String& s)
  294. {
  295.     length = s.len();
  296.     data = new char[length];
  297.     for (unsigned j = 0; j < length; j++)
  298.         data[j] = s[j];
  299. }
  300.  
  301.  
  302.  
  303. String::~String()
  304. {
  305.     delete[] data;
  306. }
  307.  
  308.  
  309.  
  310. unsigned String::len() const
  311. {
  312.     return length;
  313. }
  314.  
  315. int String::index(char c) const
  316. {
  317.     for (unsigned j = 0; j < length; j++)
  318.         if (data[j] == c) return (int)j;
  319.     return -1;
  320. }
  321.  
  322.  
  323.  
  324. void String::upcase(unsigned first, unsigned last)
  325. {
  326.     if ((first >= last) || (last > length)) throw 1;
  327.  
  328.     for (unsigned j = first; j < last; j++)
  329.         if ('a' <= data[j] && data[j] <= 'z')
  330.             data[j] -= ('a' - 'A');
  331. }
  332.  
  333. void String::downcase(unsigned first, unsigned last)
  334. {
  335.     if ((first >= last) || (last > length)) throw 1;
  336.  
  337.     for (unsigned j = first; j < last; j++)
  338.         if ('A' <= data[j] && data[j] <= 'Z')
  339.             data[j] += ('a' - 'A');
  340. }
  341.  
  342. void String::togglecase(unsigned first, unsigned last)
  343. {
  344.     if ((first >= last) || (last > length)) throw 1;
  345.  
  346.     for (unsigned j = first; j < last; j++)
  347.         if ('A' <= data[j] && data[j] <= 'Z')
  348.             data[j] += ('a' - 'A');
  349.         else if ('a' <= data[j] && data[j] <= 'z')
  350.             data[j] -= ('a' - 'A');
  351. }
  352.  
  353.  
  354.  
  355. std::ostream& operator<< (std::ostream& os, const String& s)
  356. {
  357.     if (s.len() > 0)
  358.     {
  359.         for (unsigned j = 0; j < s.len(); j++)
  360.             os << s[j];
  361.     }
  362.     else os << "";
  363.  
  364.     return os;
  365. }
  366.  
  367. std::istream& operator>> (std::istream& is, String& s)
  368. {
  369.     char* c = new char[1000];
  370.     is >> c;
  371.     s = String(c);
  372.     delete[] c;
  373.  
  374.     return is;
  375. }
  376.  
  377.  
  378.  
  379. char String::operator[] (unsigned j) const
  380. {
  381.     if (j >= length) throw 1;
  382.     return data[j];
  383. }
  384.  
  385. char& String::operator[] (unsigned j)
  386. {
  387.     if (j >= length) throw 1;
  388.     return data[j];
  389. }
  390.  
  391.  
  392.  
  393. String& String::operator= (const String& s)
  394. {
  395.     if (this == &s) return *this;
  396.  
  397.     delete data;
  398.     length = s.len();
  399.     data = new char[length];
  400.     for (unsigned j = 0; j < length; j++)
  401.         data[j] = s[j];
  402.     return *this;
  403. }
  404.  
  405.  
  406.  
  407. String& String::operator+= (const String& s)
  408. {
  409.     unsigned len = length + s.len();
  410.     char*    str = new char[len];
  411.  
  412.     for (unsigned j = 0; j < length; j++)
  413.         str[j] = data[j];
  414.  
  415.     for (unsigned i = 0; i < s.len(); i++)
  416.         str[length + i] = s[i];
  417.  
  418.     delete data;
  419.     length = len;
  420.     data = str;
  421.     return *this;
  422. }
  423.  
  424.  
  425.  
  426. String operator+ (const String& lhs, const String& rhs)
  427. {
  428.     return String(lhs) += rhs;
  429. }
  430.  
  431. String operator+ (const String& lhs, char rhs)
  432. {
  433.     return String(lhs) += String(rhs);
  434. }
  435.  
  436. String operator+ (const String& lhs, const char* rhs)
  437. {
  438.     return String(lhs) += String(rhs);
  439. }
  440.  
  441. String operator+ (char lhs, const String& rhs)
  442. {
  443.     return String(lhs) += rhs;
  444. }
  445. String operator+ (const char* lhs, const String& rhs)
  446. {
  447.     return String(lhs) += rhs;
  448. }
  449.  
  450.  
  451.  
  452. bool operator== (const String& lhs, const String& rhs)
  453. {
  454.     if (lhs.len() != rhs.len()) return false;
  455.  
  456.     unsigned cap = lhs.len();
  457.     unsigned   n = 0;
  458.     while ((n < cap) && (lhs[n] == rhs[n])) n++;
  459.     return (n == cap);
  460. }
  461.  
  462. bool operator== (const String& lhs, char rhs)
  463. {
  464.     return (lhs == String(rhs));
  465. }
  466.  
  467. bool operator== (const String& lhs, const char* rhs)
  468. {
  469.     return (lhs == String(rhs));
  470. }
  471.  
  472. bool operator== (char lhs, const String& rhs)
  473. {
  474.     return (String(lhs) == rhs);
  475. }
  476.  
  477. bool operator== (const char* lhs, const String& rhs)
  478. {
  479.     return (String(lhs) == rhs);
  480. }
  481.  
  482.  
  483.  
  484. bool operator> (const String& lhs, const String& rhs)
  485. {
  486.     unsigned cap = (lhs.len() < rhs.len()) ? lhs.len() : rhs.len();
  487.     unsigned n = 0;
  488.     while ((n < cap) && (lhs[n] == rhs[n])) n++;
  489.     if (n == cap) return (lhs.len() > rhs.len());
  490.  
  491.     if ((('A' <= lhs[n] && lhs[n] <= 'Z') || ('a' <= lhs[n] && lhs[n] <= 'z')) &&
  492.         (('A' <= rhs[n] && rhs[n] <= 'Z') || ('a' <= rhs[n] && rhs[n] <= 'z')))
  493.     {
  494.         char A = (lhs[n] & ~32);
  495.         char B = (rhs[n] & ~32);
  496.         if (A != B) return (A > B);
  497.     }
  498.     return lhs[n] > rhs[n];
  499. }
  500.  
  501. bool operator> (const String& lhs, char rhs)
  502. {
  503.     return (lhs > String(rhs));
  504. }
  505.  
  506. bool operator> (const String& lhs, const char* rhs)
  507. {
  508.     return (lhs > String(rhs));
  509. }
  510.  
  511. bool operator> (char lhs, const String& rhs)
  512. {
  513.     return (String(lhs) > rhs);
  514. }
  515.  
  516. bool operator> (const char* lhs, const String& rhs)
  517. {
  518.     return (String(lhs) > rhs);
  519. }
  520.  
  521.  
  522.  
  523. bool operator!= (const String& lhs, const String& rhs)
  524. {
  525.     return !(lhs == rhs);
  526. }
  527.  
  528. bool operator!= (const String& lhs, char rhs)
  529. {
  530.     return !(lhs == rhs);
  531. }
  532.  
  533. bool operator!= (const String& lhs, const char* rhs)
  534. {
  535.     return !(lhs == rhs);
  536. }
  537.  
  538. bool operator!= (char lhs, const String& rhs)
  539. {
  540.     return !(lhs == rhs);
  541. }
  542.  
  543. bool operator!= (const char* lhs, const String& rhs)
  544. {
  545.     return !(lhs == rhs);
  546. }
  547.  
  548.  
  549.  
  550. bool operator< (const String& lhs, const String& rhs)
  551. {
  552.     return !(lhs == rhs) && !(lhs > rhs);
  553. }
  554.  
  555. bool operator< (const String& lhs, char rhs)
  556. {
  557.     return !(lhs == rhs) && !(lhs > rhs);
  558. }
  559.  
  560. bool operator< (const String& lhs, const char* rhs)
  561. {
  562.     return !(lhs == rhs) && !(lhs > rhs);
  563. }
  564.  
  565. bool operator< (char lhs, const String& rhs)
  566. {
  567.     return !(lhs == rhs) && !(lhs > rhs);
  568. }
  569.  
  570. bool operator< (const char* lhs, const String& rhs)
  571. {
  572.     return !(lhs == rhs) && !(lhs > rhs);
  573. }
  574.  
  575.  
  576.  
  577. bool operator<= (const String& lhs, const String& rhs)
  578. {
  579.     return !(lhs > rhs);
  580. }
  581.  
  582. bool operator<= (const String& lhs, char rhs)
  583. {
  584.     return !(lhs > rhs);
  585. }
  586.  
  587. bool operator<= (const String& lhs, const char* rhs)
  588. {
  589.     return !(lhs > rhs);
  590. }
  591.  
  592. bool operator<= (char lhs, const String& rhs)
  593. {
  594.     return !(lhs > rhs);
  595. }
  596.  
  597. bool operator<= (const char* lhs, const String& rhs)
  598. {
  599.     return !(lhs > rhs);
  600. }
  601.  
  602.  
  603.  
  604. bool operator>= (const String& lhs, const String& rhs)
  605. {
  606.     return (lhs == rhs) || (lhs > rhs);
  607. }
  608.  
  609. bool operator>= (const String& lhs, char rhs)
  610. {
  611.     return (lhs == rhs) || (lhs > rhs);
  612. }
  613.  
  614. bool operator>= (const String& lhs, const char* rhs)
  615. {
  616.     return (lhs == rhs) || (lhs > rhs);
  617. }
  618.  
  619. bool operator>= (char lhs, const String& rhs)
  620. {
  621.     return (lhs == rhs) || (lhs > rhs);
  622. }
  623.  
  624. bool operator>= (const char* lhs, const String& rhs)
  625. {
  626.     return (lhs == rhs) || (lhs > rhs);
  627. }
  628.  
  629. char main_menu()
  630. {
  631.     char res;
  632.  
  633.     cout << endl << endl << endl;
  634.     cout << endl << "  ~~~  String Simulator   ~~~  ";
  635.     cout << endl << endl;
  636.     cout << "(0) Assign values to String s1, s2, and s3" << endl;
  637.     cout << "(1) Toggle Case s1, Upcase s2[0, 4], and Downcase s3[2, 4]" << endl;
  638.     cout << "(2) Copy s2 into s5 and set s2 = s2 + s4" << endl;
  639.     cout << "(3) Find 'c' in s5, -1 if not found." << endl;
  640.     cout << "(4) Add 'fooBlah' to s1" << endl;
  641.     cout << "(5) Add Random character to s4" << endl;
  642.     cout << "(6) Insert String into s5" << endl;
  643.     cout << "(7) Perform Logical Comparisons" << endl;
  644.     cout << "(8) Display Currently Registered Strings" << endl;
  645.     cout << "(9) Exit simulation" << endl << endl << endl;
  646.     cout << "Enter a selection (0-9): ";
  647.     cin >> res;
  648.     return res;
  649. }
  650.  
  651.  
  652.  
  653. void DisplayStrings(const String& s1, const String& s2, const String& s3,
  654.     const String& s4, const String& s5)
  655. {
  656.     cout << endl << "--- Current Registered Strings --- " << endl;
  657.     cout << "                   Length  Content" << endl;
  658.     cout << "        String s1:      " << s1.len() << "  " << s1 << endl;
  659.     cout << "        String s2:      " << s2.len() << "  " << s2 << endl;
  660.     cout << "        String s3:      " << s3.len() << "  " << s3 << endl;
  661.     cout << "        String s4:      " << s4.len() << "  " << s4 << endl;
  662.     cout << "        String s5:      " << s5.len() << "  " << s5 << endl;
  663. }
  664.  
  665.  
  666.  
  667. void AssignStrings(String& s1, String& s2, String& s3)
  668. {
  669.     // Assign s1 (random alphabet)
  670.     unsigned int N = rand() % 10; // Random length
  671. #define N 6
  672.     char c1[N];
  673.     for (unsigned j = 0; j<N; j++)
  674.     {
  675.         char c = (char)(rand() % 26 + 65);  // Random Alphabet
  676.         c1[j] = c;
  677.     }
  678.  
  679.     // Assign s2 (random name)
  680.     const char* names[10] = { "Alpha", "bob", "Joe", "chris", "Foo",
  681.         "foobar", "crystal", "Bart", "Bob", "Omega" };
  682.     const char* c2 = names[N];
  683.  
  684.     // Assign s3 (random ASCII)
  685.     char c3[N];
  686.     for (unsigned j = 0; j<N; j++)
  687.     {
  688.         char c = (char)(rand() % 255); // Random ASCII
  689.         c3[j] = c;
  690.     }
  691.  
  692.     s1 = c1;
  693.     s2 = c2;
  694.     s3 = c3;
  695. }
  696.  
  697.  
  698.  
  699. void LogicalTests(const String& s1, const String& s2, const String& s3,
  700.     const String& s4, const String& s5)
  701. {
  702.     cout << endl << "*** Logical Tests ***" << endl;
  703.     cout << "       s1 == s2?      " << ((s1 == s2) ? "True" : "False") << endl;
  704.     cout << "       s1 != s2?      " << ((s1 != s2) ? "True" : "False") << endl;
  705.     cout << "       s2 > s5?       " << ((s2 > s5) ? "True" : "False") << endl;
  706.     cout << "       s4 < s2        " << ((s4 < s2) ? "True" : "False") << endl;
  707.     cout << "       s1 <= fooBlah? " << ((s1 <= "fooBlah") ? "True" : "False") << endl;
  708.     cout << "       s4 >= C        " << ((s4 >= 'C') ? "True" : "False") << endl;
  709. }
  710.  
  711.  
  712. int main()
  713. {
  714.     bool isDone = false;
  715.     char choice;
  716.     String s1, s2, s3;
  717.     String s4 = '\0';
  718.     String s5;
  719.     srand(443);
  720.  
  721.  
  722.     do
  723.     {
  724.         char c = (char)(rand() % 26 + 65);
  725.         choice = main_menu();
  726.         try
  727.         {
  728.             switch (choice)
  729.             {
  730.             case '0': AssignStrings(s1, s2, s3); break;
  731.             case '1': s1.togglecase(0, s1.len());
  732.                 s2.upcase(0, 4);
  733.                 s3.downcase(2, 4); break;
  734.             case '2': s5 = s2; s2 += s4; break;
  735.             case '3': cout << endl << "Find 'c' in s5: " << s5.index('c'); break;
  736.             case '4': s1 = s1 + "fooBlah"; break;
  737.             case '5': s4[0] = c; break;
  738.             case '6': cout << endl << "Enter String into s5" << endl;
  739.                 cout << "> ";
  740.                 cin >> s5; break;
  741.             case '7': LogicalTests(s1, s2, s3, s4, s5); break;
  742.             case '8': DisplayStrings(s1, s2, s3, s4, s5); break;
  743.             case '9': isDone = true; break;
  744.             default: cout << "Invalid selection. Try again."; break;
  745.             }
  746.         }
  747.         catch (int e)
  748.         {
  749.             cout << "Error " << e << ": Index out of bounds" << endl;
  750.         }
  751.     } while (!isDone);
  752.  
  753.     return 0;
  754. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement