Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the code below, I need it to read from a file and tell if each line is a palindrome (same from left to right

In the code below, I need it to read from a file and tell if each line is a palindrome (same from left to right and right from left). Currently it only works if each line of the source file is a single word. I need it to work if each line is a sentence and ignore all punchuation. Currently, it reads the file and changes the first letter to upper case and the rest to lower. I need that to remain the same. If sends all palindromes to palindromeOut.txt and all non palindromes to quoteOut.txt. Can you help fix this code to check and entire sentence and not just a single word?

#include #include

void modtext(char line[]); int isPalindrome(char line[]);

int main(int argc, char *argv[]) {

if(argc!=2) { //Error if not filename is entered ****************** printf("Please enter filename after %s ", argv[0]); return -1; } //To open my file ******************************** FILE *fp; fp = fopen(argv[1], "r");

if(fp==NULL){ printf("Unable to open file "); return -1; } //To create the two output files that my program writes to****** FILE *fPal = fopen("palindromeOut.txt", "w"); FILE *fQuote = fopen("quotesOut.txt", "w");

char line[1000];

while(!feof(fp)) { fscanf(fp, " %[^ ]s", line); modtext(line); int isPal = isPalindrome(line); line[0] = toupper(line[0]);

if(isPal){ //To print to palindromeOut.txt *************** fprintf(fPal, "%s ", line); //To print to screen ************************** printf("Palindrome:%s ", line ); }else{ //To print to quoteOut.txt ******************* fprintf(fQuote, "%s ", line); //To print to screen ************************ printf("Quote:%s ", line ); line[0] = '\0'; } }

//To close my files *************************** fclose(fp); fclose(fPal); fclose(fQuote);

return 0;

}

void modtext(char line[]){

int i=0;

while(line[i]!='\0'){ if(line[i]>='A' && line[i]<='Z') line[i] = line[i]+32; i++; }

}

int isPalindrome(char line[]){ int i, len=0; while(line[len]!='\0') len++; for(i=0; i

}

return 1;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

(1 point) Calculate 3 sin x cos x dx.

Answered: 1 week ago