Write a program to reverse each word of a file.
#include<stdio.h> #include<string.h> void Replce_word_with_Reverse(FILE*); int main() { char file[32]; FILE *fp; printf("Enter file name = "); scanf("%[^\n]",file); fp = fopen(file,"r+"); if(fp == NULL) { printf("File is not present in this directory\n"); return 0; } Replce_word_with_Reverse(fp); return 0; } void Replce_word_with_Reverse(FILE* fp) { int i,j; char word[50],ch; while((fscanf(fp,"%s",word)) != EOF) { for(i = 0,j = ((strlen(word))-1);i<j;i++,j--) { ch = word[i]; word[i] = word[j]; word[j] = ch; } fseek(fp,-(strlen(word)),SEEK_CUR); fprintf(fp,"%s",word); } fclose(fp); printf("Reversing of word is done\n"); }
Note: File name d1 is present in the current working directory.
cat d1 Lot To Learn BlackBerry Z10 Iphone Incredible India
Output
Enter File Name = d1 Reversing Done....
cat d1 aidnI elbidercnI enohpI 01Z yrreBkcalB nraeL oT toL
Enter File Name = d1 Reversing Done....
cat d1 Lot To Learn BlackBerry Z10 Iphone Incredible India