In: Computer Science
Create a C Program that reads a file and replaces every other letter with an asterisk. The first integer 'num' is the number of lines.
Include an "Error" Message and exit the program if:
The wrong name for the input/output file is
given
The input/output file can not be opened
input.txt
7
Never
Have
We
Ever
Played
A
Game
output.txt
7
N*v*r
*a*e
W*
*v*r
*l*y*d
*
G*m*
If you have any doubts, please give me comment...
#include<stdio.h>
int main(){
char in_fname[50], out_fname[50], line[100];
int num;
printf("Enter input filename: ");
scanf("%s", in_fname);
FILE *fin = fopen(in_fname, "r");
if(fin==NULL){
printf("Error: Unable to open input file!\n");
return -1;
}
printf("Enter output filename: ");
scanf("%s", out_fname);
FILE *fout = fopen(out_fname, "w");
if(fout==NULL){
printf("Error: Unable to open output file!\n");
return -1;
}
fscanf(fin, "%d", &num);
int j = 0, k;
for(int i=0; i<num; i++){
fscanf(fin, "\n%[^\n]s", line);
k = 0;
while(line[k]!='\0'){
if(j%2!=0)
line[k] = '*';
k++;
j++;
}
fprintf(fout, "%s\n", line);
}
fclose(fout);
fclose(fin);
return 0;
}