go to previous page   go to home page   go to next page

Answer:

It seems reasonable to scan through the string character by chracter until a punctuation character is found.


Early Return from a Loop

Although not structured, the following is a reasonable solution

int containsPunct( char *p )
{
  while ( *p )
  {
    if ( ispunct( *p ) ) return 1;
    p++ ;
  }
  return 0;
}

The strictly structured version may not be as clear:

int containsPunct( char *p )
{
  while ( *p && !ispunct( *p ) )
  {
    p++ ;
  }
  return ispunct( *p );
}

The while statements in both versions scan through the string until the null-byte is reached. Recall that null counts as "false" and everything else counts as "true." The structured version expects ispunct( int c ) to return 0 when its argument is 0.

Which version to use is a design decision. But it should be a decision, not the result of rushing in without thought and accepting whatever comes out.


QUESTION 9:

What if the requirements for your program change, and now you are supposed to count the number of punctuation characters in a string?