The if structure is similar and seems to work just fine. The program completely skips over the while structure though.
Is this an appropriate way to use conditional structures? I imagine this could be considered poorly readable code but I wouldn't know.
Code:
/*
8.16
Write a program that inputs a line of text and a search string from the
keyboard. Using function strstr, locate the first occurrence of the search
string in the line of text, and assign the location to variable searchPtr of
type char *. If the search string is found, print the remainder of the line of
text beginning with the search string. Then, use strstr again to locate the
next occurrence of the search strig in the line of text. If a second occurrene
is found, print the remainder of the line of text begiining with the seond
occurrence. (Hint: The second call to strstr should contain searchPtr + 1 as
its first argument. )
*/
#include <stdio.h>
#include <string.h>
int main()
{
char *searchPtr;
char bulk[ 100 ] = { };
char searchString[ 100 ] = { };
printf( "Enter the initial string: " );
scanf( "%s", bulk );
printf( "Enter string to search for: " );
scanf( "%s", searchString );
if ( searchPtr = strstr( bulk, searchString ) ) {
printf( "%s\n", searchPtr );
while ( searchPtr = strstr( searchPtr + 1, searchString ) ) {
printf( "%s\n", searchPtr );
}
}
// Test commands below to pinpoint problem
//searchPtr = strstr( searchPtr + 1, searchString );
//printf( "%s\n", searchPtr );
return 0;
}