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.
#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;
}
Comments
Leave a comment