-4

then it has to be printed to the screen 80 chars per line.

this is done in c.

My coding is super weak and don't know where to begin.

Any help is appreciated

zvbat
  • 3
  • 3
  • 1
    'don't know where to begin.' => first try reading data from a file, there are **loads** of examples of how to do this – smoggers Sep 03 '16 at 18:17
  • 1
    This is a basic programming question, not a specific problem with a specific solution. Google is your friend for this. Look up how to read from a file, how to output to the screen, etc. – J Earls Sep 03 '16 at 18:24
  • http://stackoverflow.com/q/26243115/971127 , http://stackoverflow.com/questions/23618316/undefined-reference-to-strlwr – BLUEPIXY Sep 03 '16 at 18:44
  • @BLUEPIXY: I don't see reason to use `strtok` for this. That's a simple filter, no need for context. – too honest for this site Sep 03 '16 at 18:49
  • @Olaf _That's a simple filter_ sure :-). – BLUEPIXY Sep 03 '16 at 18:55

1 Answers1

1

given the requirement of change upper case to lower case, will need:

#include <ctype.h>

for the tolower() function and isalnum() function

............

to input the characters from the file, and since each char needs to be processed, will need:

#include <stdio.h>

for the function: getchar() and for the definition of EOF and for the function: putc()

.............

To count the number of characters currently displayed on the current line, will need:

size_t lineLen = 0;

................

to know when to go to the next output line will need that number 80

#define MAX_LINE_LEN (80)

.........

If the command line contains:

myprogram < inputFile.txt

then will not need to open/close the file within the program.

...........

naturally, will need a main() function with no parameters:

int main( void )
{
...
} // end function: main

.........

Since we will be reading a lot of characters, one at a time, will need:

The following while() statement edited so can handle any input char.

int ch;
while( (ch = getchar()) != EOF )
{
...
}

........

Since only displaying printable characters, will need a line like:

if( isalnum( ch ) )
}
....
}

..........

Since displaying only in lower case will need a line like:

ch = tolower( ch );

............

need to actually output the char, and track the length of the line

putc( ch );
lineLen++;

............

need to output a new line after 80 chars and reset counter

if( MAX_LINE_LEN >= lineLen )
{
    lineLen = 0;
    putc( '\n' );
}

............

After the EOF is encountered, will need to output a final newline so all characters are displayed

putc( '\n' );

............

That is really all there is to it.

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • `while( (ch = getchar()) && EOF != ch )` stops the `while` loop should `ch` have the value of 0 or EOF. It that your intent? – chux - Reinstate Monica Sep 03 '16 at 23:29
  • "After the EOF is encountered, will need to output a final newline" --> Only need to print if `lineLen > 0`; – chux - Reinstate Monica Sep 03 '16 at 23:32
  • @chux, If the input file is a text file, then there will never be a input char of `0x00`, so that will not be a problem. I agree about the 'rare' case of zero characters to be printed, however; outputting a final newline will not hurt and the OP can always add a check for zero characters. I did not try to write the program for the OP, just get the OP started in the correct direction. – user3629249 Sep 05 '16 at 14:01
  • Nice answer in not giving it all away. Detail: C does not define/limit the contents of a text file, so "If the input file is a text file, then there will never be a input char of 0x00" sounds more like opinion than C fact. – chux - Reinstate Monica Sep 05 '16 at 15:52
  • @chux, I modified the `while()` statement so it can input any character – user3629249 Sep 06 '16 at 09:41