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. )

Do a google search for "implement my own strstr function" you will find lots of suggestions

Just tried that, wasn't helpful at all. I need someone to try to explain it to me in english/pseudocode.

Is length of str1 less than length of str2? If so, exit with not found.
Look through the characters in str2 for the first character in str1.
If not found, exit, otherwise...
Are the next chars in str2 the same as str1?

Is length of str1 less than length of str2? If so, exit with not found.
Look through the characters in str2 for the first character in str1.
If not found, exit, otherwise...
Are the next chars in str2 the same as str1?

Oh wow, when you put it like that, it seems so simple. Thank you,

This article has been dead for over six months. Start a new discussion instead.