Pages

Friday, 9 February 2018

C 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.



C 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>

int main()
{
                FILE *fs;
                char ch,s[80];
                void rev();
        fs=fopen("sample.txt","r");
                if(fs==NULL)
                {
                                printf("File not found!");
                                return 0;
                }
                while(fgets(s,79,fs)!=NULL)
                                rev(s);
                fclose(fs);
        return 0;
}

void rev(char s1[80])
{
                char s2[80];
                int i=0,j=0;
        clrscr();
                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));
}