Question

In C++: You are to write two functions, printString() and testString(), which are called from the...

In C++:

You are to write two functions, printString() and testString(), which are called from the main function.

  1. printString (string) prints every character in the string to std::cout with a space after every character and a newline at the end.
  2. testString (string) returns true if the string contains characters that are in sorted order, false otherwise. You may assume that all characters are lowercase and only alphabetical characters are present. See the main() to see how the two functions are called.

Some sample runs are given below:

  • string: “hippy”
  • printString prints: h i p p y
  • testString returns: true (since ‘h’ <= ‘i' <= ‘p’ <= ‘p’ <= ‘y’)
  • string: “world”
  • printString prints: w o r l d
  • testString returns: false
  • string: “effort”
  • printString prints: e f f o r t
  • testString returns: true

Note:

  • Write only the two functions in the space provided.
  • You can change the main function for your own testing. Your code will be tested with a similar main function.

Hints:

  • You can access the i-th character of a string s with s[i]. You can compare characters using the <= operator.
  • The number of characters in the string is s.size()

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;
bool checkAnswer(const string &nameOfTest, bool received, bool expected);

// Implement printString here

????

// Implement testString here

????

// EDIT CODE BELOW *ONLY* FOR TESTING (ANY CODE BELOW WILL BE OVER-WRITTEN DURING GRADING WITH DIFFERENT TESTS)

int main() {
    cout << "Test if the string contains characters that are in sorted order" << endl;
    {
      string s = "hippy";
      cout << "Contents of array : ";
      printString(s);
      checkAnswer(s, testString(s), true);
    }
    {
      string s = "world";
      cout << "Contents of array : ";
      printString(s);
      checkAnswer(s, testString(s), false);
    }
    {
      string s = "effort";
      cout << "Contents of array : ";
      printString(s);
      checkAnswer(s, testString(s), true);
    }
    {
      string s = "a";
      cout << "Contents of array : ";
      printString(s);
      checkAnswer(s, testString(s), true);
    }
    {
      string s = "able";
      cout << "Contents of array : ";
      printString(s);
      checkAnswer(s, testString(s), false);
    }
    //   system("pause"); // comment/uncomment if needed

    return 0;
}

bool checkAnswer(const string &nameOfTest, bool received, bool expected) {
      if (received == expected) {
          cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
          return true;
      }
      cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
      return false;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

bool checkAnswer(const string &nameOfTest, bool received, bool expected);

// Implement printString here
void printString(const string &s) {
    for (int i = 0; i < s.size(); ++i) {
        cout << s[i] << " ";
    }
    cout << endl;
}

// Implement testString here
bool testString(const string &s) {
    for (int i = 0; i < s.size() - 1; ++i) {
        if (s[i] > s[i + 1]) {
            return false;
        }
    }
    return true;
}

// EDIT CODE BELOW *ONLY* FOR TESTING (ANY CODE BELOW WILL BE OVER-WRITTEN DURING GRADING WITH DIFFERENT TESTS)
int main() {
    cout << "Test if the string contains characters that are in sorted order" << endl;
    {
        string s = "hippy";
        cout << "Contents of array : ";
        printString(s);
        checkAnswer(s, testString(s), true);
    }
    {
        string s = "world";
        cout << "Contents of array : ";
        printString(s);
        checkAnswer(s, testString(s), false);
    }
    {
        string s = "effort";
        cout << "Contents of array : ";
        printString(s);
        checkAnswer(s, testString(s), true);
    }
    {
        string s = "a";
        cout << "Contents of array : ";
        printString(s);
        checkAnswer(s, testString(s), true);
    }
    {
        string s = "able";
        cout << "Contents of array : ";
        printString(s);
        checkAnswer(s, testString(s), false);
    }
//   system("pause"); // comment/uncomment if needed

    return 0;
}

bool checkAnswer(const string &nameOfTest, bool received, bool expected) {
    if (received == expected) {
        cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
        return true;
    }
    cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
    return false;
}

Add a comment
Know the answer?
Add Answer to:
In C++: You are to write two functions, printString() and testString(), which are called from the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are to write two functions, printString() and testString(), which are called from the main function....

    You are to write two functions, printString() and testString(), which are called from the main function. printString (string) prints characters to std::cout with a space after every character and a newline at the end. testString (string) returns true if the string contains two consecutive characters that are the same, false otherwise. See the main() to see how the two functions are called. Some sample runs are given below: string: “hello” printString prints: h e l l o testString returns: true...

  • Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive...

    Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive integers in the linked list are equal (return true in this case, false otherwise). You may assume that the list is not empty. A recursion “helper” function is already included in class IntegerLinkedList. You only need to write the recursive function. For example, checkRecurseHelper() should return true for the linked list shown below. A main function (prob3.cpp) is given to you to add data...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • Using C++ Use the below program. Fill in the code for the 2 functions. The expected...

    Using C++ Use the below program. Fill in the code for the 2 functions. The expected output should be: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: #include #include #include using namespace std; //Assume a line is less than 256 //Prints the characters in the string str1 from beginIndex //and inclusing endIndex void printSubString( const char* str1 , int beginIndex, int endIndex ) { } void printWordsInAString( const char* str1 ) { } int main() { char str1[] = "The fox jumps over the...

  • You are given a partial implementation of two classes. GroceryItem is a class that holds the...

    You are given a partial implementation of two classes. GroceryItem is a class that holds the information for each grocery item. GroceryInventory is a class that holds an internal listing of GroceryItems as well as the tax rate for the store. Your inventory could be 100 items it could be 9000 items. You won’t know until your program gets the shipment at runtime. For this you should use the vector class from the C++ standard library. I've completed the GroceryItem.h...

  • You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function...

    You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function into a file named Q1.cpp. Q1.cpp should only include your function implementation, the necessary #include directives if needed, and should not contain anything else such as the main function or global variable declarations. Test your code using a separate main.cpp file where you implement a sufficient number of test cases. You should use Q1.h as a header file and Q1main.cpp as a main function...

  • Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from...

    Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from the user. Then it determines if one string is a permutation of the other string. If the program answers "yes" to the previous question, meaning the two strings are permutations of each other, determine if each string has all unique characters. --------------------------------------------------------------------------------- I have completed the first part but I am unsure on how to also determine if each string is all unique characters...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT