Levels Tought:
Elementary,Middle School,High School,College,University,PHD
Teaching Since: | Jul 2017 |
Last Sign in: | 266 Weeks Ago, 3 Days Ago |
Questions Answered: | 1850 |
Tutorials Posted: | 1850 |
Graduate in Biology and Nutrition, MBA Finance
Florida State University
Aug-2000 - Jul-2007
Ass. Relationship Manager
Penn-Florida
Mar-2009 - Feb-2016
Hi,
I’m doing a problem currently and I am being asked to create a program which opens a file the user enters and searches that file for words that end in ed. I’m trying to make it so that the program changes the characters to all lower case before it searches so that words ending in ED, Ed, and Ed will also be returned.
#includeÂ
#includeÂ
#includeÂ
#includeÂ
#define Max_Lines 1000
#define Max_Length 500
int read_file( const char *filename, char* word_array[] ); // function to read the file
void lower_string( char* word_array[] );
int main( void )
{
int i;
int number_of_lines;
int length;
char filename[ 300 ];
char* word_array[ Max_Lines ];
int opened = 0;
while ( opened != 1 ){
printf( "Please enter the file name you wish to search:n" );
scanf("%s",filename);
if ( -1 == ( number_of_lines = read_file( filename, word_array ) ) ){
printf( "The file name entered could not be opened.n" );
opened = 0;
}
else
{
opened = 1;
}
}
printf( "nThe words ending in "ed" are as follows:n" );
for ( i = 0; i length = strlen( &word_array[ i ][ 0 ] );
if ( strcmp( &word_array[ i ][ length - 2 ], ( "ed" ) ) == 0 ) {
printf( "%sn", &word_array[ i ][ 0 ] );
}
}
return 0;
}
void lower_string( char* word_array[] )
{
int i;
while ( word_array[ i ] != '' ) {
for ( i = 0; word_array[ i ]; i++ ) {
putchar( tolower( word_array[ i ] ) );
}
}
return i;
}
int read_file( const char *filename, char* word_array[] )
{
char buffer[ Max_Length ];
int i = 0, length;
FILE * filePtr;
filePtr = fopen( filename, "r" );
if ( !filePtr ) {
return -1;
}
while ( !feof( filePtr ) ){
fscanf( filePtr, "%s", buffer );
length = strlen( buffer );
word_array[ i ] = ( char* )malloc( length + 1 );
strcpy( word_array[ i ], buffer );
++i;
}
fclose( filePtr );
return i;
}
Hel-----------lo -----------Sir-----------/Ma-----------dam----------- Â----------- -----------Tha-----------nk -----------you----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------acq-----------uis-----------iti-----------on -----------of -----------my -----------pos-----------ted----------- so-----------lut-----------ion-----------.Pl-----------eas-----------e p-----------ing----------- me----------- on----------- ch-----------at -----------I a-----------m Â----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I