Pages

Friday, 9 February 2018

Write a program to carry out the following: (a) Read a text file ‘INPUT.TXT’ (b) Print each word in reverse order Example, Input: INDIA IS MY COUNTRY Output: AIDNI SI YM YRTNUOC Assume that each word length is maximum of 10 characters and each word is separated by newline/blank characters.


Write a program to carry out the following:
(a) Read a text file ‘INPUT.TXT’
(b) Print each word in reverse order
Example,
Input: INDIA IS MY COUNTRY
Output: AIDNI 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<conio.h>
#include<string.h>

void main() {


FILE *fs;
char s[80];
void rev();

clrscr();

fs=fopen("INPUT.TXT","r");

if(fs==NULL) {

printf("cannot open file!");
exit();
}


while(fgets(s,79,fs)!=NULL)
rev(s);

fclose(fs);

getch();

}

void rev(char s1[80]) {

char s2[80];
int i=0,j=0;

while(s1[i]!='\0') {

s2[j]=s1[i];

if(s1[i]==' ' || s1[i]=='\0') {

s2[j]='\0';

strrev(s2);

printf("%s ",s2);

j=-1;

}

i++;
j++;
}

s2[j]='\0';

printf("%s",strrev(s2));


}