/* WAP to read data from file and replace all vowels with white spaces */
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch,file[50];
int i=0;
clrscr();
fp=fopen("ajay.txt","r");
if(fp==NULL)
{
printf("Error while opening file\n Exiting in 3 seconds");
delay(3000);
exit(0);
}
else
{
printf("\n *********** File data ************\n");
while(1)
{
ch=getc(fp);
printf("%c",ch);
if(ch==EOF)
{
file[i]='\0';
break;
}
else
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
file[i]=' ';
}
else
{
file[i]=ch;
}
}
i=i+1;
}
}
fclose(fp);
fp=fopen("ajay.txt","w");
fprintf(fp,"%s",file);
fclose(fp);
printf("\n******** Output file ***********\n");
fp=fopen("ajay.txt","r");
while(1)
{
ch=getc(fp);
if(ch==EOF)
{
break;
}
else
{
printf("%c",ch);
}
}
fclose(fp);
getch();
}
IMP :
First create simple text file in Turbo C's "Bin" folder using notepad. Write something in it.
Here I created "ajay.txt".
Its important because turbo c's compiler access file by default from "Bin".
Explaination:
In this program we first read data from file "ajay.txt" and store it in string named "file".
while storing file data to string we also check for vowels, and we stores that vowel as a white space(' ').
then we used fprintf() to store output string to file directly.(we can store that string to file with one by one character using getc() and putc(), but we have predefined function for that so we used fprintf(), which is efficient and easy).
And finally we displayed file content on console. :)
If you have different and efficient approach to this program you can mail it to me from below contact form. Your program will be published here.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch,file[50];
int i=0;
clrscr();
fp=fopen("ajay.txt","r");
if(fp==NULL)
{
printf("Error while opening file\n Exiting in 3 seconds");
delay(3000);
exit(0);
}
else
{
printf("\n *********** File data ************\n");
while(1)
{
ch=getc(fp);
printf("%c",ch);
if(ch==EOF)
{
file[i]='\0';
break;
}
else
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
file[i]=' ';
}
else
{
file[i]=ch;
}
}
i=i+1;
}
}
fclose(fp);
fp=fopen("ajay.txt","w");
fprintf(fp,"%s",file);
fclose(fp);
printf("\n******** Output file ***********\n");
fp=fopen("ajay.txt","r");
while(1)
{
ch=getc(fp);
if(ch==EOF)
{
break;
}
else
{
printf("%c",ch);
}
}
fclose(fp);
getch();
}
IMP :
First create simple text file in Turbo C's "Bin" folder using notepad. Write something in it.
Here I created "ajay.txt".
Its important because turbo c's compiler access file by default from "Bin".
Explaination:
In this program we first read data from file "ajay.txt" and store it in string named "file".
while storing file data to string we also check for vowels, and we stores that vowel as a white space(' ').
then we used fprintf() to store output string to file directly.(we can store that string to file with one by one character using getc() and putc(), but we have predefined function for that so we used fprintf(), which is efficient and easy).
And finally we displayed file content on console. :)
If you have different and efficient approach to this program you can mail it to me from below contact form. Your program will be published here.
No comments:
Post a Comment