Question

I need help programming this program in C. 1.) Write a program that reads a message,...

I need help programming this program in C.

1.) Write a program that reads a message, then checks whether it's a palindrome (the letters in the message are the same from left to right as from right to left), example is shown below:

Enter a message: He lived as a devil, eh?

Palindrome

Enter a message: Madam, I am Adam.

Not a Palindrome

2.) Ignore all characters that aren't letters. Use integer variables to keep track of positions in the array.

3.) It must include and implement the following function prototypes:

  • void get_msg(char [], int *);
  • bool palindrome(char [], int);

4.) The main() of this program will call get_msg() to obtain only the alphabet characters of the entered message and the total number of alphabet characters of the message. These two data are returned by get_msg() via its two parameters. Here 'alphabet characters' include both upper and lower case of 26 letters only.

  • Hint 1: Use getchar() would be easier to process the entered message because it makes easier to catch and test one character a time until Enter is hit. When a character is caught, you would save it in an array only if it is an alphabet character. There are many examples of getchar() in the book, use index to help you to find them if you never use it.
  • Hint 2: To check if a character is an alphabet character, you can use isalpha(), which is defined in ctype.h. See p.612-5 for examples.
  • Hint 3: C is case sensitive, so, 'a' and 'A' are different characters while they are treated as the same letter in palindromes (i.e., palindromes are case insensitive). You may consider to use toupper() or tolower(), which is also defined in ctype.h, to solve the problem.

5.) This program defines a macro MAX_MSG_LEN with a value of 80, which is used to limit the total alphabet characters that can be saved in an array.

6.) The main() will pass both data received from get_msg() to palindrome(), which returns either true or false, depending on if the array contents is a palindrome. Based on this return, main() prints a simple message, 'Palindrome' or 'Not a palindrome', as shown above.

7.) Please follow all directions.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution to above problem in C. Please read the code comments for more information

C CODE

#include <stdio.h>
#include<string.h>
#include<ctype.h>
#define MAX_MSG_LEN 80
void get_msg(char msg [], int * len)
{
int i=0;
char c;
do
{
c=getchar();
//checking if alpha
if(isalpha(c))
{
   //converting to lower to ignore case sensitivity
msg[i++]= tolower(c);
}
  
}while(c!='\n');
msg[i]='\0';
*len =i;
  
}
//to check palindrome
bool palindrome(char msg[],int * len)
{
int start=0;
int end=*len-1;
int i=0;
//comparing start to end
while(start<=end)
{
   //if not matching send false;
if(msg[start]!=msg[end])
return false;
start++;
end--;
}
return true;
}
int main()
{
char msg[MAX_MSG_LEN];
int len=0;
printf("Enter a message: ");
get_msg(msg,&len);
if(palindrome(msg,&len))
printf("Palindrome\n");
else
printf("Not a Palindrome\n");
return 0;
}

SCREENSHOT OF OUTPUT

Add a comment
Know the answer?
Add Answer to:
I need help programming this program in C. 1.) Write a program that reads a message,...
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
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