Write a code to accept an input stream of no more than 60 characters and test wh
ID: 3631414 • Letter: W
Question
Write a code to accept an input stream of no more than 60 characters and test whether the input stream forms a numerical palindrome. The code should ignore all characters that are not digits (so spaces, punctuation marks, letters, etc). Here is the beginning:#include <stdio.h>
#include <stdlib.h>
#include <cytpe.h>
#define max_length 60
void main()
{
int c;
int input_chars[max_length];
int length;
printf ("Input the test phrase: ");
length = 0;
/* limit the length of the phrase to the first 60 chars */
/* do not store any non-digit input */
while ((c = getchar()) != ' ' && length < max_length) {
if (isdigit(c)) {
input_chars[length] = c - 48;
length++;
}
}
Complete the code in the following manner:
1. Print first all the numbers (digits) stored in the input_chars[] array.
2. Test the array for the presence of a numerical palindrome.
3. Double check to make sure it works!
Thank you in advance for your help! :)
Explanation / Answer
please rate - thanks
using your code
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define max_length 60
int main()
{
int c;
int input_chars[max_length];
int length,i,j,good=1;
printf ("Input the test phrase: ");
length = 0;
/* limit the length of the phrase to the first 60 chars */
/* do not store any non-digit input */
while ((c = getchar()) != ' ' && length < max_length) {
if (isdigit(c)) {
input_chars[length] = c - 48;
length++;
}
}
j=length-1;
for(i=0;i<length/2;i++)
{if(input_chars[i]!=input_chars[j])
good=0;
j--;
}
printf("The numbers ");
for(i=0;i<length;i++)
printf("%d",input_chars[i]);
if(good==1)
printf(" are a palindrone ");
else
printf(" are not a palindrone ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.