Skip to content

Instantly share code, notes, and snippets.

@bleaktradition
Created June 27, 2018 18:38
Show Gist options
  • Save bleaktradition/266b1cd5ec863259beb2e4eb2d004a96 to your computer and use it in GitHub Desktop.
Save bleaktradition/266b1cd5ec863259beb2e4eb2d004a96 to your computer and use it in GitHub Desktop.
checking for palindrom using for or while loop
#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;
}
#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