my code works when I try to find repeating 3's (2) times. But I need to
be able to find the patterns for any number of times. I am kind of lost on where to go from here. I know I am a little sloppy here, but maybe you can
give me some direction?
Hello,
could you explain a bit more the meaning of the digitPatternCount?
The statement dynamicArray[index + 1 - tempPatternCount] seems dangerous to me when digitPatternCount is > 1 (and index reaches 0).
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <stdlib.h>
usingnamespace std;
int analyzePatternDigitJeremyM(int arg);
int main(void) {
int a = 1333343333;
int b = -1333343333;
analyzePatternDigitJeremyM(a);
analyzePatternDigitJeremyM(b);
return 0;
}
int analyzePatternDigitJeremyM(int arg) {
// transform the arg in order to process it more easily
int positiveForm = abs(arg);
stringstream theStream;
theStream << positiveForm;
string reverseStringForm = theStream.str();
reverse(reverseStringForm.begin(),reverseStringForm.end());
// inputs
char digitPatternNumber = ' ';
int digitPatternCount=0;
cout << "please enter the digit to be found in a pattern" << endl;
cin >> digitPatternNumber;
cout << "please enter the amount of digits in the pattern to be found." << endl;
cin >> digitPatternCount;
string thePattern(digitPatternCount,digitPatternNumber);
//inputs validation should be checked here: digitPatternCount > 0
// search the first occurence of thePattern
string::size_type index = reverseStringForm.find(thePattern);
bool isTheLoopFinished = (index == string::npos);
while (not isTheLoopFinished) {
reverseStringForm.at(index+digitPatternCount-1) = '0';
// search the next occurence of the Pattern
index = reverseStringForm.find(thePattern,index+digitPatternCount);
isTheLoopFinished = (index == string::npos);
}
//reverses the arg again, and outputs it
reverse(reverseStringForm.begin(),reverseStringForm.end());
cout << reverseStringForm << endl;
}
I hope this helps.
I'am not sure I've answered your question in fact.