int palindrome(char * str){
char * pt1 = str;
char * pt2 = str;
if(pt1==NULL){
std::cout<<"\n invalid string";
return -1;
}
while(*pt2 != '\0')
pt2++;
pt2--;
while(pt1 < pt2){ // why does this work correctly ?
if(*pt1 != *pt2){
std::cout<<"\n not palindrome";
return 0;
}
else{
pt1++;
pt2--;
}
}
std::cout<<"\n yes palindrome";
return 1;
}
hello
this is a function to check if the passed char* points to a palindrome/not.
here two pointers
pt1 – starts from begining moves fwds
pt2 – starts from end moves backwards
now i do not want them to continue once they meet in the middle..
SO i check if always pt1
why? i am not comparing *pt1 vs *pt2.
what values does it compare ?
These are the three concepts leading this block of code executing correctly:
Due to the fact that pointers are really just integers (such as 0x000001, 0x000002 etc) you can use comparison operators on them.
Lastly due to the fact that the memory for the string will be continuous and increasing, there is a level of abstraction that you can make: Pointers further along in memory will evaluate as greater than addresses earlier in memory.