1. Definition:
We will set a function to reverse an integer and set
another function to test whether a string is palindrome.
A palindrome is a string of characters that is read
the same forward or backward. The name "ajaja" is palindrome ..
2. The method in C language:1. The reverse function
#include<stdio.h>
#include<math.h>
int get_number_digits(int n)
{
int count = 0;
do
{
++count;
n /= 10;
}
while(n != 0);
return count;
}
int main()
{
int number, reversed, next,i;
int gamma1, gamma2;
printf ("Enter a positive integer: --> :");
scanf("%d", &number);
reversed = 0;
int size = get_number_digits(number);
//printf("n The size is %d n", size);
for (i=1; i<=size; i++)
{
gamma1 = pow(10,(i-1));
gamma2 = pow(10,i);
next = (number/gamma1) - (number/gamma2*10);
reversed = (reversed * 10) + next;
}
printf("n The entered number is %d . Its reversed is %d n", number, reversed);
return 0;
}
2. Palindrome function
#include<stdio.h>
#include<string.h>
int main()
{
char str;
char * string = &str;
int size;
printf("n Enter a string: --> ");
scanf("%s", &str);
size = strlen(string);
char tempo[size];
strcpy(tempo,string);
strrev(tempo);
if(strcmp(string,tempo) == 0)
printf("n The entered string "%s" is palindrome. n",string);
else
printf("n The ntered string "%s" is not palindrome. n",string);
printf("n The reversed string "%s" is n", strrev(string));
return 0;
}
/*
Notes:
----------
"strlen" returns the length of a string.
"strcpy" copies a string to another.
"strcmp" compares two strings.
"strrev" reverses a string.
*/