The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-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;
}
-----------