I need to do task:
Write a function to check if the given natural number has the property that it is read from the beginning and from the end. The number should be passed to the function as int. Tip: Replace the number with an array of characters (string).
I did it, but... don't compile. I now, there is some stupid mistake, that i don't see. Please, look at my code:
#include <stdio.h>
#include <string.h.>
#include <stdbool.h>
//Function is checking if number put to the string in string is this one, that we want to find
bool check(char *number);
int i;
{
int leng=(int)strlen(liczba);
for( i=0;i<leng/2;i++)
if(number[i]!=number[leng-i-1])return false; //compare number from the beginning to thih from the
end
//if they are differetn, then false
return true; //thye are this same, so true
}
int main()
{
int how_many=0;
char number[20];
printf("Give your number(max 20 digits") );
scanf(" %d", &number);
if(check(number))
printf("Number has this same value");
else printf("Number hasn't this same value");
}
Errors:
#include <stdio.h>
#include <string.h.> /* extra . is here */
#include <stdbool.h>
//Function is checking if number put to the string in string is this one, that we want to find
bool check(char *number); /* extra ; is here */
int i; /* place of variable declaration is wrong */
{
int leng=(int)strlen(liczba); /* undeclared "liczba" is used */
for( i=0;i<leng/2;i++)
if(number[i]!=number[leng-i-1])return false; //compare number from the beginning to thih from the
end /* extra newline is before "end" */
//if they are differetn, then false
return true; //thye are this same, so true
}
int main()
{
int how_many=0;
char number[20]; /* have no room for terminating null-character */
printf("Give your number(max 20 digits") ); /* position of ")" is wrong */
scanf(" %d", &number); /* format specifier is wrong and extra & exists */
if(check(number))
printf("Number has this same value");
else printf("Number hasn't this same value");
}
Fixed code:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
//Function is checking if number put to the string in string is this one, that we want to find
bool check(char *number)
{
int i;
int leng=(int)strlen(number);
for( i=0;i<leng/2;i++)
if(number[i]!=number[leng-i-1])return false; //compare number from the beginning to thih from the end
//if they are differetn, then false
return true; //thye are this same, so true
}
int main()
{
int how_many=0;
char number[21];
printf("Give your number(max 20 digits)" );
scanf(" %20s", number);
if(check(number))
printf("Number has this same value");
else printf("Number hasn't this same value");
}