C File Handling Programs 13

Question:25
Write a C program to read a file word by word. Further display each word,its reverse, and its length

example:
file:test10.txt
contents:
this is a good file THIS
how are you This very good Amit amit aMita tHis
Sol:

 

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
   FILE *fp;
   char buff[255];
   int i=1;
   fp = fopen("test10.txt", "r");
   while(1)
   {
   fscanf(fp, "%s", buff); //read only 1st word
    if(feof(fp))
   {
     printf("\n\n\nend of file");
     break;
   }
   //printf("%s ",strrev(buff));
   printf("word : %s ",buff);
   printf("reverse : %s length : %d\n",strrev(buff),strlen(buff));
   }
   fclose(fp);
   getch();
   return(0);
}
/* Output */
word : this reverse : siht length : 4
word : is reverse : si length : 2
word : a reverse : a length : 1
word : good reverse : doog length : 4
word : file reverse : elif length : 4
word : THIS reverse : SIHT length : 4
word : how reverse : woh length : 3
word : are reverse : era length : 3
word : you reverse : uoy length : 3
word : This reverse : sihT length : 4
word : very reverse : yrev length : 4
word : good reverse : doog length : 4
word : Amit reverse : timA length : 4
word : amit reverse : tima length : 4
word : aMita reverse : atiMa length : 5
word : tHis reverse : siHt length : 4

end of file