Question: write C++ code 3 Consecutive integers You are given a string str. You need to implement a function to test whether str is formed by
write C++ code
3 Consecutive integers You are given a string str. You need to implement a function to test whether str is formed by consecutive integers (without separators). The integers are in the standard decimal format. You may assume integers are non-negative. For example, str = 123467 is not formed by consecutive integers, while str = 1213141516 is (i.e., 12, 13, 14, 15, 16). You may assume integers are at most 999999.
starter code
//
// ECConsecutiveInts.cpp
//
//
// Created by Yufeng Wu on 1/15/23.
//
//
#include
#include
using namespace std;
// Test whether strInput is a string composed of consecutive and increasing integers (in decimal formats)
// Return true if yes and false otherwise (return false if there are unexpected characters)
// For example, 1234578 would return false, while 1213141516 would return true (12 13 14 15 16)
// You may assume the integers is at most 999999 and there is no any seperators between numbers
// You may also assume integers are all non-negatives
// Tip: try to use library functions instead of writing a lot of code yourself
// functions in C++ string you may find useful:
// substr: extract a substring from a string
// stoi: convert string to integer (be careful about how to handle exception)
// and so on..
bool ECConsecutiveInts(const string &strInput)
{
// your code here
}
test code
// To build: c++ ECConsecutiveIntsTest.cpp ECConsecutiveInts.cpp -o test
#include
#include
using namespace std;
extern bool ECConsecutiveInts(const string &strInput);
int main()
{
// Not consecutive
string a = "1234578";
bool fa = ECConsecutiveInts(a);
if(fa)
{
cout << "Consecutive ";
}
else
{
cout << "NOT Consecutive ";
}
// consecutive
string b = "111213141516";
bool fb = ECConsecutiveInts(b);
if(fb)
{
cout << "Consecutive ";
}
else
{
cout << "NOT Consecutive ";
}
// NOT consecutive
string c = "123124125127";
bool fc = ECConsecutiveInts(c);
if(fc)
{
cout << "Consecutive ";
}
else
{
cout << "NOT Consecutive ";
}
// NOT consecutive
string d = "123a567";
bool fd = ECConsecutiveInts(d);
if(fd)
{
cout << "Consecutive ";
}
else
{
cout << "NOT Consecutive ";
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
