Hey everybody, here's my assignment.
"Program 69: String Pattern Matching – Naïve Approach
Write a program that will prompt the user to enter a string of characters. There will be a maximum of 10 characters entered for the string. The program will then ask the user to enter lines of text. A line is terminated with a \n character. After each line is entered, your program should determine if the pattern exists within the line. Is the pattern a substring of the line? If it is, then you should output “Yes” and then you should identify the starting position in the line where the pattern matches. Do this for each line. The end of the output is signified by an empty line. The user hits the carriage return without entering anything. Your program should not call the strstr function. I want you to create your own strstr function. You can call it mystrstr."
And here's what I have for code so far.
/*************************************************************/
/* Programmer: Brendon O'Connell */
/* */
/* Program 69: String Pattern Matching - Naive Approach */
/* */
/* Approximate Completion Time: x minutes */
/*************************************************************/
#include <stdio.h>
/* int mystrstr( char str1[], char str2[] ) { */
/* if( ) printf( "\nYes!\n" ) ; */
/* else printf( "\nNo match was found. Please try again.\n" */
/* } */
int main( int argc, char *argv[] ) {
char str1[11], line[100], yorn ;
int cont ;
int i = 0 ;
printf( "Please enter a string of no more than ten characters.\n" ) ;
while( str1[ i - 1 ] != '\n' ) {
scanf( "%c", &str1[i] ) ;
i++ ;
}
printf( "\nPlease press y followed by enter to continue, or EOF at any time to quit.\n" ) ;
while( scanf( "%d", &cont ) != EOF ) {
printf( "\nPlease enter a line of text.\n" ) ;
while( line[ i - 1 ] != '\n' ) {
scanf( "%c", &line[i] ) ;
i++ ;
}
}
return 0 ;
}
All that being said, I just have one question:
How do I even begin to write mystrstr?
( A function that detects if a smaller string is a substring of a larger string. )