Read a text file containing several occurrences of student and replace the word student with graduate.
#include<stdio.h>
#include<string.h>
int main(){
FILE *fp;
char ch;
char str[1000];
int i=0;
FILE *fp1;
fp = fopen("test.txt","r");
fp1= fopen("test1.txt","w+");
ch=fgetc(fp);
while(ch!=EOF){
if(ch == '\n'){
i++;
str[i] = '\0';
i = 0;
char *a = NULL;
char b[9]="graduate";
a = strtok(str," \n");
while(a!=NULL){
if(strncmp(a,"student",8) == 0){
fprintf(fp1,"%s ",b);
}
else{
fprintf(fp1,"%s ",a);
}
a = strtok(NULL," \n");
}
fprintf(fp1,"%c",'\n');
}
else{
str[i] = ch;
i++;
}
ch =fgetc(fp);
}
fclose(fp);
fclose(fp1);
return 0;
}
#include<stdio.h>
#include<string.h>
int main(){
FILE *fp;
char ch;
char str[1000];
int i=0;
FILE *fp1;
fp = fopen("test.txt","r");
fp1= fopen("test1.txt","w+");
ch=fgetc(fp);
while(ch!=EOF){
if(ch == '\n'){
i++;
str[i] = '\0';
i = 0;
char *a = NULL;
char b[9]="graduate";
a = strtok(str," \n");
while(a!=NULL){
if(strncmp(a,"student",8) == 0){
fprintf(fp1,"%s ",b);
}
else{
fprintf(fp1,"%s ",a);
}
a = strtok(NULL," \n");
}
fprintf(fp1,"%c",'\n');
}
else{
str[i] = ch;
i++;
}
ch =fgetc(fp);
}
fclose(fp);
fclose(fp1);
return 0;
}
No comments:
Post a Comment