Created
June 27, 2018 18:38
-
-
Save bleaktradition/266b1cd5ec863259beb2e4eb2d004a96 to your computer and use it in GitHub Desktop.
checking for palindrom using for or while loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> | |
int main (int argc, char ** argv) { | |
char name[] = {"rentne"}; | |
char * a = &name[0]; // set to first letter in name | |
char * b = &name[sizeof(name)-2]; // set to last letter in name | |
std::cout << "Name entered: " << name << std::endl; | |
for (int i=0; i<(sizeof(name)/2); i++) { | |
std::cout << "a: " << *a << "\nb: " << *b << std::endl; | |
if (*a != *b) { | |
std::cout << "Not a palindrom" << std::endl; | |
exit(0); | |
} | |
a++; | |
b--; | |
} | |
std::cout << "It's a palindrom" << std::endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> | |
bool diff_char (char * a, char * b); | |
int main (int argc, char ** argv) { | |
char name[] = {"rentner"}; | |
char * a = &name[0]; // set to first letter in name | |
char * b = &name[sizeof(name)-2]; // set to last letter in name | |
bool check = true; | |
int counter = 0; | |
std::cout << "Name entered: " << name << std::endl; | |
while (check == true && counter < sizeof(name)/2) { | |
check = diff_char(a, b); | |
a++; b--; counter++; | |
} | |
std::cout << "It's a palindrom" << std::endl; | |
return 0; | |
} | |
bool diff_char (char * a, char * b) { | |
std::cout << "a: " << *a << "\nb: " << *b << std::endl; | |
if (*a != *b) { | |
std::cout << "Not a palindrom" << std::endl; | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment