/** * Assignment #5 * Program to find repeated characters. * Author: Mark Allen Weiss * SSN: 000-00-0000 * Course: COP-2210 Section 8 * Date: October 26, 1998 * * CERTIFICATION OF SOLE AUTHORSHIP * I certify that this work is solely my own and * that none if it is the work of any other person. * I further certify that I have not provided any * assistance to other students enrolled in COP-2210 that * would render their CERTIFICATION OF SOLE AUTHORSHIP * invalid. I understand that violations of this pledge * may result in severe sanctions. * * * * ----------------------------------- * (Mark Allen Weiss) <--- Put your name here, and sign above */ #include #include "apstring.h" const int EMPTY_STRING_POS = -1; int IndexOfDuplicates( apstring str ); void Process( apstring str ); int main( ) { apstring str; Process( "aardvark" ); Process( "access" ); Process( "" ); Process( "3432433312" ); cout << "Enter a string: "; cin >> str; Process( str ); return 0; } /** * Find the most consuctive identical characters. * Return the index where these characters start. */ int IndexOfDuplicates( apstring str ) { int maxDuplicates = 0; int currentDuplicates = 0; int index = 0; int i = 0; int j; if( str == "" ) return EMPTY_STRING_POS; for( i = 0; i < str.length( ); i = j ) { for( j = i + 1; j < str.length( ) && str[ j ] == str[ i ]; j++ ) ; // At the end of the loop, j is at the first non-matching character currentDuplicates = j - i; if( currentDuplicates > maxDuplicates ) { maxDuplicates = currentDuplicates; index = i; } } return index; } /** * Print out the return value from IndexOfDuplicates and * the position that is repeated. */ void Process( apstring str ) { int pos = IndexOfDuplicates( str ); if( pos == EMPTY_STRING_POS ) cout << "Empty string; return value is " << pos << endl; else { cout << "For " << str << " the maximal duplicate sequence starts at " << pos << ", which is a '" << str[ pos ] << "'" << endl; } }