Write a C program to reverse the first n characters in a file, where n is given by the user.




Algorithm:
1. Start
2. read the command line arguments
3. check if arguments=3 or not, If not print invalid no of arguments
4. open source file in reading mode
5. if the NULL pointer, then the print file cannot be open
6. Store no of chars to reverse in k
7. K= *argv[2]-48
7. read the item from file stream using fread
8. Store chars from the last position to initial position in another string(temp)
9. print the temp string
10. Stop




Program Code:
#include <stdio.h> 
#include<string.h> 
int main() 
FILE *fp; 
char a[50]; 
int i,n,count=0; 
fp=fopen("program.txt","r"); 
if(fp==NULL) 
printf("\n file opening problem"); 
return; 
printf("\n enter the no of charcters to reverse: "); 
scanf("%d",&n); 
while(count<n) 
a[count]=fgetc(fp); 
count++; 
a[count]='\0'; 
fputs(strrev(a),stdout); 
fclose(fp); 
getch(); 
}





Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: