You will read in a file supplied from the commandline (filename.ext,) and write/overwrite out a file which will be called (filename.ext.mng.)

This file will be essentially the same as the original file, but will have all characters on all lines the reverse of the original. For example:
The original file has:
Jack Spratt
could eat no fat.
His wife
could eat no lean.
The output file will look like:
ttartpS kcaJ
.taf on tae dluoc
efiw siH
.nael on tae dluoc

Recommended Answers

All 3 Replies

>any pointers on how to do this
Yes, start working on the section that will turn a given string into what you want. Once you grasp the concept of reversing the string, you can start with the whole reading/writing to file busyness.

ok heres my code to reverse the string.

int main() {
 char string[256];
 int len;

 
 fgets(file, 256-1, stdin);
 len = (strlen(string) - 1);
 printf("You entered %s \nlength %d:\n", file, len);

 printf("Reverse string:");
 for(; len >= 0; len--)
        printf("%c",file[len]);

 printf("\n");

}

char string[256]; fgets(file, 256-1, stdin); Don't you mean string?
If you pass to fgets the size of the buffer as a second argument, you don't need to subtract -1 for the '\0'; fgets() always do that automatically.
Better yet, instead of using a magic number of 256-1, say sizeof string len = (strlen(string) - 1); strlen() upon success returns the length of the string without counting the string terminator.
Subtracting -1 will be in most cases where the \n or ENTER key lives.
Can you think of an instance when that will not be the case? printf("You entered %s \nlength %d:\n", file,len); There's not such an identifier named file. printf("%c",file[len]); Once again file does not exist.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.