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

Code implemented in c

Comments are written for code explanation

Filename: program.c

#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>

#define MAX_MSG_LEN 80

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

void get_msg(char message[], int *length){
char ch;
int count = 0;

printf("Enter a message: ");
while((ch = getchar()) != EOF && ch != '\n') {
if (tolower(ch) >= 'a' && toupper(ch) <= 'z'){
message[count] = tolower(ch);
count++;
}
}
*length = count;
}

bool palindrome(char message[], int length){
char *ptr_start = message, *ptr_end = ptr_start + length - 1;

while(ptr_start < ptr_end){
if (*ptr_start++ != *ptr_end--)
return false;
}
return true;
}

int main(void){
char message[MAX_MSG_LEN];
int *length = (int*)malloc(sizeof(message));
get_msg(message, length);
printf("%salindrome\n", palindrome(message, *length) ? "P" : "Not a p");
free(length);
return 0;
}

Code Screenshots:

Working Code Output Screenshots:

If you like my answer, hit thumbs up. Thank you.

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
  • Write a program in C that reads a string of bits( so either a one or...

    Write a program in C that reads a string of bits( so either a one or zero) in from the user one char at a time using the function getChar, which returns a char. hint in order to convert a char to an int, subtract the character. Then store the bits into an array. start with this: #include "stdio.h" #define MAX_BITS 32 int main() { printf("Enter up to 32 bits (hit 'enter' to terminate early): "); char bit = getchar();...

  • Write a program palind.c that reads a message, then checks whether it's palindrome (the letters in...

    Write a program palind.c that reads a message, then checks whether it's palindrome (the letters in the message are the same from left to right as from right to left): Enter a message He lived as a devil, eh? Palindrome Enter a message Madam, I am Adam Not a palindrome Ignore all characters that aren't letters. Use integer variables to keep track of positions in the array. Revise the program in Part I to use a pointer instead of an...

  • Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To...

    Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To compile, build, and execute an interactive program with a simple loop, conditions, user defined functions and library functions from stdio.h and ctype.h. You will write a program that will convert characters to integers and integers to characters General Requirements • In your program you will change each letter entered by the user to both uppercase AND lowercase– o Use the function toupper in #include...

  • Can you help me make these two methods listed below work? I have code written but...

    Can you help me make these two methods listed below work? I have code written but it is not working. I would appreciate any advice or help. Function "isAPalidrome" accepts an array of characters and returns an integer. You must use pointer operations only, no arrays. This function should return 1 (true) if the parameter 'string' is a palindrome, or 0 (false) if 'string' is not a palindrome. A palindrome is a sequence of characters which when reversed, is the...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt...

    Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt a message is to use a date’s 6 digits to shift the letters. For example, if a date is picked as December 18, 1946, then the 6 digits are 121846. Assume the dates are in the 20th century. To encrypt a message, you will shift each letter of the message by the number of spaces indicated by the corresponding digit. For example, to encrypt...

  • 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...

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
ADVERTISEMENT