Question

Using C language Palindromes A palindrome is a word or phrase that reads the same backwards...

Using C language

Palindromes

A palindrome is a word or phrase that reads the same backwards and forwards, ignoring punctuation, spaces, and anything that isn’t a letter.

Examples of Palindromes

  • Madam, I’m Adam.
  • Kayak
  • Racecar
  • A man, a plan, a canal – Panama!
  • Able was I, ere I saw Elba.
  • Go hang a salami, I’m a lasagna hog!

Write a program that does the following:

  1. opens a text file named “Proj3input.txt” containing multiple lines of text (1 or more)
  2. for each line, print out the condensed version of that line of text with all non-alphabetic characters removed, and all alphabetic characters turned into lower case.
  3. for each line, print out “Palindrome:” and then the line of text if it IS a palindrome, and print out “Not a palindrome:” and the line of text if it is NOT a palindrome.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<stdio.h>

#include<string.h>

#include<ctype.h>

//method to remove all non alphabetic characters from a line and store in second parameter

void removeNonAlpha(char *line, char* result){

                int len=strlen(line);

                //looping through the original line

                for(int i=0;i<len;i++){

                                char c=line[i];

                                //checking if c is alphabet

                                if(isalpha(c)){

                                               //converting to lower case, appending to result

                                               c=tolower(c);

                                               strncat(result,&c,1);

                                }

                }

}

//method to check if a line is palindrome (line is formatted prior to the call

//to this method)

int isPalindrome(char* line){

                int len=strlen(line);

                //looping for len/2 times

                for(int i=0;i<len/2;i++){

                                //comparing i'th character from front and rear

                                char c1=line[i];

                                char c2=line[len-1-i];

                                //if they are not same, returning 0 (not palindrome)

                                if(c1!=c2){

                                               return 0;

                                }

                }

                //if tests passed successfully, returning 1

                return 1;

}

int main(){

                //opening Proj3input.txt file

                FILE* file=fopen("Proj3input.txt","r");

                //printing error if file not found

                if(file==NULL){

                                printf("File not found!\nPlease check if Proj3input.txt exists on the same directory\n");

                                return 0;

                }

                char line[150];

                //reading file line by line

                while(fgets(line, 150, file) != NULL){

                                char formatted[150]="";

                                //formatting line (removing non alphabetic chars)

                                removeNonAlpha(line,formatted);

                                //checking if formatted line isPalindrome or not

                                if(isPalindrome(formatted)==1){

                                               printf("Palindrome: %s\n",formatted);

                                }else{

                                               printf("Not a palindrome: %s\n",formatted);

                                }

                }

                //closing file

                fclose(file);

                return 0;

}

/*OUTPUT*/

Palindrome: madamimadam

Not a palindrome: cprogramming

Palindrome: kayak

Not a palindrome: hello

Palindrome: racecar

Palindrome: amanaplanacanalpanama

Palindrome: ablewasiereisawelba

Palindrome: gohangasalamiimalasagnahog

Not a palindrome: haha

//Proj3Input.txt file that I used

Madam, I’m Adam.

C programming

Kayak

Hello

Racecar

A man, a plan, a canal – Panama!

Able was I, ere I saw Elba.

Go hang a salami, I’m a lasagna hog!

Haha

Add a comment
Know the answer?
Add Answer to:
Using C language Palindromes A palindrome is a word or phrase that reads the same backwards...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Programming Palindrome Detector: A palindrome is any word, phrase, or sentence that reads the same...

    C++ Programming Palindrome Detector: A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man,a plan, a canal, Panama Desserts, I stressed Kayak Write a book function that uses recursion to determine if a string argument is a palindrome. The function should return true if the argument reads the same forward and backward. Demonstrate the function in a program, which continues to...

  • A palindrome is a word or phrase that reads the same both forwards and backwards. "racecar"...

    A palindrome is a word or phrase that reads the same both forwards and backwards. "racecar" is a palindrome. So is, "a man a plan a canal, panama" if you ignore the spacing and the comma. Discovering how to automate the detection of palindromes is a good way to test your understanding of loops and strings. The file, "hw2problem5.py", contains a function called is_palindrome(), which has a single string parameter, s. If s is a palindrome, then is_palindrome() returns the...

  • A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...

    A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. In this program, ask the user to input some text and print out whether or not that text is a palindrome. Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome. Create the method reverse...

  • C Program: 6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and...

    C Program: 6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and backward. Some examples of palindromes are: "radar" "able was i ere i saw elba" and, if you ignore blanks: "a man a plan a canal panama" Write a recursive function testPalindrome that returns 1 if the string stored in the array is a palindrome and 0 otherwise. The function should ignore spaces and punctuation in the string. Use the function in a complete program...

  • Problem 1: (Palindromes) A palindrome is a string that's spelled the same way forward and backward....

    Problem 1: (Palindromes) A palindrome is a string that's spelled the same way forward and backward. Some examples of palindromes are; radar, able was i ere i saw elba; and, if you ignore blanks, a man a plan a canal panama .Write a recursive function testPa1indrome that returns 1 if the string stored in the array is a palindrome and 0 otherwise. The function should ignore spaces and punctuation in the string. Please I need it in C program and...

  • I really need help with the code for this, in C++ please, I know there's a...

    I really need help with the code for this, in C++ please, I know there's a similar question but the answer isn't correct :[ A palindrome is a string that reads the same forwards and backwards (ignoring spaces, punctuation, and capitalization). Examples are the familiar ``If I had a hi-fi,'' the grander ``A man, a plan, a canal, Panama,'' and ``Some men interpret nine memos.'' So the goal: Design, implement, document, and test a program that reads a line of...

  • 1 of 1 15 points) Palindrome Detector. A palindrome is a phrase that reads the Pi...

    1 of 1 15 points) Palindrome Detector. A palindrome is a phrase that reads the Pi same ronwards as it does backwards. For example, "a man, a plan, a canal, Panama is a palindrome. Write a program that uses a stack to check for palindromes in each line of a text file. Try your program on the example text file "palindromes txt".Your program should output the palindromes that it finds in the document. For example: FindPalindromes( palindromes.txt") will display "a...

  • This is a java question A palindrome is a word or phrase that reads the same...

    This is a java question A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and punctuations, and considering uppercase and lowercase versions of the same letter to be equal. For example, the following are palindromes: warts n straw radar Able was I ere I saw Elba tacocat Write a program named Palindrome.java that will accept a file (file name) from the command argument list and decide whether each line in the file is...

  • Write a python program that contains a hardcoded string and checks whether it is a palindrome...

    Write a python program that contains a hardcoded string and checks whether it is a palindrome (the same forwards as backwards). If it is then display “It is a palindrome”, otherwise display “It is not a palindrome”. A hardcoded string is simply a string defined in your program, for example: my_string = “Hello” Some sample palindromes to use are: A car, a man, a maraca Desserts, I stressed Madam, I’m Adam You will need to strip out any punctuation such...

  • Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward...

    Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT