Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cs-142:just-palindromes [2015/10/14 17:06] (current)
kseppi created
Line 1: Line 1:
 +<code cpp>
  
 +#include <​iostream>​
 +#include <​string>​
 +
 +using namespace std;
 +
 +/*
 + Recursive function to find palindrome
 + @input string to test if it's a palindrome
 + ​@return true if string is a palindrome, false otherwise
 + */
 +bool is_palindrome(string str)
 +{
 +    // base case
 +    if (str.length() <= 1)
 +    {
 +        return true;
 +    }
 +    ​
 +    // if first and last letter are the same
 +    if (str.substr(0,​ 1) != str.substr(str.length() - 1))
 +    {
 +        return false;
 +    }
 +    else{
 +        // and the rest of me is a palindrome
 +        return is_palindrome(str.substr(1,​ str.length() - 2));
 +    }
 +}
 +
 +int main()
 +{
 +    cout << "Word: ";
 +    string input;
 +    getline(cin,​input);​
 +    ​
 +    if (is_palindrome(input))
 +    {
 +        cout << "​IT'​S A PALINDROME!!!"​ << endl;
 +    }
 +    else
 +    {
 +        cout << "not a palindrome"​ << endl;
 +    }
 +    ​
 +    char ch;
 +    cin >> ch;
 +    return 0;
 +}
 +
 +</​code>​
cs-142/just-palindromes.txt ยท Last modified: 2015/10/14 17:06 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0