Question

how do i Write a C program called palindromes that prints all integers between 0 and...

how do i Write a C program called palindromes that prints all integers between 0 and 1000000 whose squares are palindromes, along with their squares. A palindrome is a word that is the same as its reverse (e.e. radar). You must do this in the following way.
  1. First write a function void strrev(char *r, char *s) that reverses the string s, putting the result into r. Hint: the function strlen returns the length of a string. The function should assume that the calling function has allocated enough space for r.
  2. Next write a function int ispalindrome(char *s) that returns 1 if s is a palindrome and 0 otherwise. Hints: the function strcmp compares two strings. Read the manual page to find out how it works. You must create a string large enough to hold the reverse. Do this by dynamically allocating the string with malloc. After comparing the string to its reverse, free the memory that you allocated.
  3. Your main program will test every integer from 0 to 1000000, printing out the number (and its square) if it is a palindrome. To use the ispalindrome function, it must convert the integer to a string. Hint: use sprintf to do this. sprintf is like printf except that it has an additional first argument that is a string. It "prints" the number into the string instead of to standard output. Hint: Declare the string variable in the main program as an array of character. Use a size large enough to hold all the digits.
calvin 8:38pm > ./palindromes
0 0
1 1
2 4
3 9
11 121
22 484
26 676
...

it has to be a C program
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include<stdio.h>

#include<string.h>

void strrev(char *r, char *s);

int ispalindrome(char *s);

int main(){

int i;

char s[100], sq[100];

for(i=0; i<1000000; i++){

sprintf(s, "%d", i);

sprintf(sq, "%d", (i*i));

if(ispalindrome(sq))

printf("%s %s\n", s, sq);

}

return 0;

}

void strrev(char *r, char *s){

int len = strlen(s);

int i=0;

while(s[i]!='\0'){

r[len-i-1] = s[i];

i++;

}

r[len] = '\0';

}

int ispalindrome(char *s){

char rev[100];

strrev(rev, s);

if(strcmp(rev, s)==0)

return 1;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
how do i Write a C program called palindromes that prints all integers between 0 and...
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