Apr 19, 2011

find maximum length of space in given text

void maxlenofspaces(char a[])
{
int count = 0;
int maxcount = 0;
int start = 0;
for(int i=0; a[i] != '\0'; i++)
{
if(a[i] == ' ')
{
if(!count)
start = i;
count++;
if(count > maxcount)
maxcount = count;
}
else
{
start = i-count-1;
count = 0;
}

}

cout << "\nstart index=" << start << " max spaces length=" << maxcount << endl;
}

No comments: