i've been working on this project for the past few days and i cant seem to get it to work...
the function find_word is supposed to determine if one character string exists inside another string...so far it works, only if the the first word entered doesnt have double letters...
for example...if the first word is ''bookkeeper'', and the word you are looking for is ''keep'', my function won't find it because it only checks ''bookkeeper'' beginning with the first ''k''.
how do i get my program to work, even with words with double
letters...any help will be greatly appreciated
Code:
#include <stdio.h>
/* Function prototypes */
int find_word ( char word[], char to_find[] ) ;
int string_compare ( char compare[], char to_find[] ) ;
void get_string( char buffer[], int size ) ;
const int MAX = 30 ; /* Max size of array */
/*
Name : get_string - Allows the user to input a string.
Parameters : buffer - The input string.
*/
void get_string( char buffer[], int size )
{
char character;
int j = 0;
do /* Get a character until newline or */
{ /* we run out of characters. */
character = getchar() ;
buffer[j] = character ;
++j;
}
while ( character != '\n' && j < size ) ;
while ( character != '\n' ) /* Get rid of extra characters. */
character = getchar() ;
buffer[j - 1] = '\0' ; /* Replace newline with the null-byte. */
}
void main ( void )
{
char word[ MAX ] ; /* Word to search */
char to_find[ MAX ] ; /* Word to search for */
int found_word ; /* Flag */
printf( "Enter a word\t : " ) ; /* Prompt user to enter a word */
get_string( word, MAX ) ; /* Get the word */
printf( "Enter another word : " ) ; /* Prompt user to enter another word */
get_string( to_find, MAX ) ; /* Get the word */
/* Check if the word to find is in the first word */
found_word = find_word( word, to_find ) ;
if( found_word == -1 ) /* If the word isn't found, display the results */
printf( "\n%s was not found in %s.\n", to_find, word ) ;
else /* If it is found, display the index found at */
printf( "\n%s was found beginning at index %d.\n", to_find, found_word ) ;
}
/*
Parameters : word[] - The word to search
: to_find[] - The word to search for
*/
int find_word ( char word[], char to_find[] )
{
int i, j = 0 ; /* Loop counters */
char compare[ MAX ] ; /* Word to compare to to_find */
int are_equal ; /* Flag */
for( i = 0 ; word[ i ] != '\0' ; i++ ) /* Cycle through the array looking for the */
if( to_find[ j ] == word[ i ] ) /* first letter of the word to_find. */
{ /* If the letter is found, */
for( j = 0 ; to_find[ j ] != '\0' ; j++ ) /* copy the dimension of elements of to_find in */
compare[ j ] = word[ i + j ] ; /* word to compare. */
compare[ j ] = '\0' ;
are_equal = string_compare( compare, to_find ) ;
if( are_equal == 0 )
return ( -1 ) ;
else
return ( i ) ;
}
}
/*
Parameters : word, string2 - The strings to compare.
Returns : 1 if the strings are equal, 0 if not.
*/
int string_compare ( char compare[], char to_find[] )
{
int j = 0 ; /* Loop counter. */
int are_equal ; /* Flag. */
/* Compare the strings. */
while ( compare[j] == to_find[j] && compare[j] != NULL && to_find[j] != NULL )
++j ;
if ( compare[j] == NULL && to_find[j] == NULL )
are_equal = 1 ; /* If we've made it to the end of */
else /* both strings, they're equal. */
are_equal = 0 ; /* Otherwise, they're not. */
return ( are_equal ) ; /* Return to main. */
}