/* program to
write a sentence is one file and print the entire sentence
to the second
file by reversing the non palindrome word only*/
#include<stdio.h>
#include<string.h>
int palin(char
c[15]);
int main()
{
FILE *fp,*fq;
char
c='a',ct[15];
int i,len,j;
fp=fopen("first.txt","w");
if(fp==NULL)
{
printf("\n ERROR opening
file:");
}
else
{
printf("\n Enter the text:");
while(c!='\n')
{
scanf("%c",&c);
fprintf(fp,"%c",c);
}
}
fclose(fp);
fq=fopen("first.txt","r");
fp=fopen("second.txt","w");
if(fq==NULL)
{
printf("\n ERROR opening
file:");
}
else
{
while(!feof(fq))
{
i=0;
c=fgetc(fq);
while(c!=' ' &&
c!='\n')
{
ct[i]=c;
i++;
c=fgetc(fq);
if(c==EOF)break;
}ct[i]='\0';
if(palin(ct)==0)
{
fputs(ct,fp);
fputc(' ',fp);
}
else
{
len=strlen(ct);
for(j=len-1;j>=0;j--)
{
fputc(ct[j],fp);
}
fputc(' ',fp);
}
}
}
fcloseall();
fp=fopen("second.txt","r");
if(fp==NULL)
printf("\n ERROR OPENING
FILE:");
else
{
printf("\n Text is the second
file:\n");
while(!feof(fp))
{
fscanf(fp,"%c",&c);
printf("%c",c);
}
}
fcloseall();
return 0;
}
int palin(char c[])
{
int len,i,j=0;
char p[15];
len=strlen(c);
for(i=len-1;i>=0;i--)
{
p[j]=c[i];
j++;
}
p[j]='\0';
if(strcmp(p,c)==0)
return 0;
else
return 1;
}
No comments:
Post a Comment