Question

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.

  1. printString (string) prints characters to std::cout with a space after every character and a newline at the end.
  2. 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 (because of ‘l’ and ‘l’)
  • string: “world”
  • printString prints: w o r l d
  • testString returns: false
  • string: “hello world”
  • printString prints: h e l l o   w o r l d
  • testString returns: true (because of ‘l’ and ‘l’)

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().

prob1.cpp

#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 two consecutive characters that are the same" << endl;
{
string s = "hello";
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 = "hello world";
cout << "Contents of array : ";
printString(s);
   checkAnswer(s, testString(s), true);
}
{
string s = "zzz";
cout << "Contents of array : ";
printString(s);
   checkAnswer(s, testString(s), true);
}
{
string s = "snakess";
cout << "Contents of array : ";
printString(s);
   checkAnswer(s, testString(s), true);
}
{
string s = "a";
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);


void printString(string s)
{
    for(int i=0;i<s.size();i++)
    {
        std::cout <<s[i]<<" ";
    }
}

bool testString(string s)
{
    bool flag=false;
    
    for(int i=1;i<s.size();i++)
    {
        if(s[i]==s[i-1])
        {
            flag=true;
            break;
        }
    }
    
    return flag;
}

int main() {
cout << "Test if the string contains two consecutive characters that are the same" << endl;
{
string s = "hello";
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 = "hello world";
cout << "Contents of array : ";
printString(s);
   checkAnswer(s, testString(s), true);
}
{
string s = "zzz";
cout << "Contents of array : ";
printString(s);
   checkAnswer(s, testString(s), true);
}
{
string s = "snakess";
cout << "Contents of array : ";
printString(s);
   checkAnswer(s, testString(s), true);
}
{
string s = "a";
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:
You are to write two functions, printString() and testString(), which are called from the main function....
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
  • 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. printString (string) prints every character in the string to std::cout with a space after every character and a newline at the end. 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...

  • 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...

  • 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 to implement a MyString class which is our own limited implementation of the std::string...

    You are to implement a MyString class which is our own limited implementation of the std::string Header file and test (main) file are given in below, code for mystring.cpp. Here is header file mystring.h /* MyString class */ #ifndef MyString_H #define MyString_H #include <iostream> using namespace std; class MyString { private:    char* str;    int len; public:    MyString();    MyString(const char* s);    MyString(MyString& s);    ~MyString();    friend ostream& operator <<(ostream& os, MyString& s); // Prints string    MyString& operator=(MyString& s); //Copy assignment    MyString& operator+(MyString&...

  • Call each of the print functions in the main() using string double-pointers in the main void...

    Call each of the print functions in the main() using string double-pointers in the main void print(string ** s){             **s = "Hello";             cout<< **s <<endl; } void print(string *x){             *x = "World";             cout<<*x<<endl; }

  • 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...

  • roblem description Write the following functions as prototyped below. Do not alter the function signatures. Demonstrate...

    roblem description Write the following functions as prototyped below. Do not alter the function signatures. Demonstrate each function using literal strings defined in your client program (e.g., don't prompt the user for test inputs) /// Returns a copy of the string with its first character capitalized //I and the rest lowercased /// Example: capitalize("hELLO WORLD") returns "Hello world std::string capitalize(const std::string&str) /// Returns a copy of the string centered in a string of length ·width.. /// Padding is done using...

  • 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