// lengthLongest.cpp
// Tests whether a command-line argument
// contains any non-ASCII characters.
// Uses a for loop.
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
if ( argc < 2 )
{
// Announce purpose of program:
cout << "Run this program with a single "
<< "command-line argument." << endl;
cout << "You will be told the length of the "
<< "longest sequence of identical characters." << endl;
return 0;
} // if
int lengthLongest = 0; // length of longest sequence of
// identical characters so far.
int lengthCurrent = 0; // length of current sequence of
// identical characters, if any.
char previousCharacter = '\0'; // No real previous character yet.
int index = 0;
while ( argv[1][index] != '0' )
{
if ( argv[1][index] == previousCharacter )
{
lengthCurrent++;
if ( lengthCurrent > lengthLongest )
lengthLongest = lengthCurrent;
} // if ( argv[1][index] == previousCharacter )
else
lengthCurrent = 0;
// Next pair of characters:
previousCharacter = argv[1][index];
index++;
} // while
// Output result:
cout << "Your command-line argument ";
if ( nonAsciiFound )
cout << "contains at least one non-ASCII character.";
else
cout << "does not contain any non-ASCII characters.";
cout << endl;
return 0;
} // function main