Where did I go wrong? I\'ve written the code but can\'t get it to \"print the ou
ID: 3536407 • Letter: W
Question
Where did I go wrong? I've written the code but can't get it to "print the output to screen" - in other words, the code doesn't display the change from lower case letters to upper case letters. I don't need the program to display the letters, character or letter counts.
Here's the assignment:
Have a program prompt the user for a filename to open. Change every alphabetic character in the file to a capital letter. Numbers and special characters should not be changed. Print the output to the screen.
Here's what I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define BUFLEN 128
int main()
{
char line[ BUFLEN ],
filename[ BUFLEN ],
c,
*p;
int chars = 0, letters = 0, lines = 0;
FILE* inFile;
printf( "Please enter the name of a file: " );
fflush( stdout );
fgets( filename, 128, stdin );
p = strchr( filename, ' ' );
if( p ) * p = '';
else while( getchar() != ' ' ) ;
inFile = fopen( filename, "r" );
if( inFile )
{
while( fgets( line, BUFLEN, inFile ) )
{
++lines;
printf( "%s", line );
}
rewind( inFile );
printf( " File contents with line numbered: " );
while( ( c = fgetc( inFile )) != EOF )
{
++chars;
if( isalpha( c ) )
{
printf( " %d : ", chars );
++letters;
putchar(c );
}
}
fclose( inFile );
}
else
{
printf( " The file %s was not successfully opened.", filename );
printf( " Please make sure the specified file exists. " );
}
printf( " lines = %d, chars = %d, letters = %d ", lines, chars, letters );
printf( "Press 'Enter' to continue/exit ... " );
fflush( stdout );
getchar();
return 0;
}
Explanation / Answer
printf( " The file %s was not successfully opened.", filename ); should be printf( " The file %s was not successfully opened.",tolower(filename );
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.