Answer to Question #207422 in C for Zeeshan

Question #207422

Write a program to carry out the following:

(a) Read a text file ‘INPUT.TXT’

(b) Print each word in reverse order

Example,

Input: PAKISTAN IS MY COUNTRY

Output: NATSIKAP SI YM YRTNUOC

Assume that each word length is maximum of 10 characters and each word is

separated by newline/blank characters.


1
Expert's answer
2021-06-18T05:47:57-0400
#include <stdio.h>
#include <string.h>


int main()
{
    FILE *f;
    char buf[11];
    int i, len;


    f = fopen("INPUT.TXT", "r");
    if (!f) {
        return 1;
    }


    while (fscanf(f, "%s", buf) == 1) {
        len = strlen(buf);
        for (i=len-1; i>=0; --i) {
            printf("%c", buf[i]);
        }
        printf(" ");
    }
    printf("\n");
    fclose(f);


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS