Question:23
Write a C program to read a file word by word. Further, reverse the contents of the file word by word. each word is displayed in separate line.
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("%d : %s\n",i++,strrev(buff)); } fclose(fp); getch(); return(0); }
/* Output */ 1 : siht 2 : si 3 : a 4 : doog 5 : elif 6 : SIHT 7 : woh 8 : era 9 : uoy 10 : sihT 11 : yrev 12 : doog 13 : timA 14 : tima 15 : atiMa 16 : siHt end of file
Question:24
Write a C program to read a file word by word. Further, reverse the contents of the file word by word
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("%s ",strrev(buff)); } fclose(fp); getch(); return(0); }
/* Output */ siht si a doog elif SIHT woh era uoy sihT yrev doog timA tima atiMa siHt end of file