Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I modify my code (see below) to meat the a and b requirements?: a) Modify the function that removes ignores non-letters (i.e., the

How can I modify my code (see below) to meat the a and b requirements?:

a) Modify the function that removes ignores non-letters (i.e., the one that removes spaces and special characters from a string). You have probably defined this as a void-function since it does not need to return any value. Your task is to change the function so that it now returns how many characters that were removed by the function (or 0 if no character were removed). (2p) b) Modify main() so that it displays information of how many characters were removed in order to verify if it was a palindrome. See the example run how this could be displayed (2p)

Example run (user input inbold,comments not partof the output initalic): Enter a sentence: A lemon one space will be removed This sentence is not a palindrome. 1 character(s) were removed. Do you want to enter another sentence (0 for no, 1 for yes)? 1 Enter a sentence: No lemon, no melon. three spaces and two special characters removed This sentence is a palindrome. 5 character(s) were removed. Do you want to enter another sentence (0 for no, 1 for yes)? 0

My code:

#include #include #include #define SIZE 100 int isPalindrome(char inputString[]) { int flag = 0; int i = 0; int length; length = strlen(inputString); int last = length - 1; int reverse[SIZE]; for (i = 0; i < length; i++) { reverse[i] = inputString[last]; last--; } for (i = 0; inputString[i] != '\0'; i++) { if (inputString[i] != reverse[i]) { return 0; } } return 1; } void ignoreNonLetters(char inputString[]) { int count = 0; int i = 0; int length; length = strlen(inputString); for (i = 0; i < length; i++) { if (isalpha(inputString[i])) { inputString[count] = inputString[i]; count++; } } inputString[count] = '\0'; } int ignoreUpper(char inputString[]) { int i = 0; while (inputString[i]) { inputString[i] = tolower(inputString[i]); i++; } } int main(void) { int choice = 1; while (choice == 1) { int palindrome; char inputString[SIZE]; printf("Enter a sentence: "); gets(inputString); ignoreNonLetters(inputString); ignoreUpper(inputString); printf("%s", inputString); palindrome = isPalindrome(inputString); do { if (palindrome == 1) { printf(" The sentence is a palindrome"); } else { printf(" This sentence is not a palindrome"); } printf(" Do you want to enter another sentence (0 for no, 1 for yes) ? : "); scanf_s("%d", &choice); } while (getchar() != ' '); } return 0; }

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

Online Market Research Cost Effective Searching Of The Internet And Online Databases

Authors: John F. Lescher

1st Edition

0201489295, 978-0201489293

More Books

Students also viewed these Databases questions