Recursions
Nov 8, 2015 at 11:29pm UTC
Hi all, I am creating a program that uses recursion functions to count the vowels in a sentence and to determine if it is a palindrome. The problem I am getting is that it says the sentence entered is not palindrome even if it is.. Any help with this will be greatly appreciated. Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#include<iostream>
#include <cmath>
using namespace std;
struct Sentence
{
int CountVowels(string , int );
public :
Sentence (string);
bool isPal(string , int );
void Print();
string s;
int numVowel;
int length;
//~Sentence();
};
Sentence :: Sentence (string b)
{
s = b;
length = 0;
numVowel = CountVowels(s, 0);
}
int Sentence :: CountVowels(string myWord, int startindex)
{
length ++;
int pandi;
if (myWord[startindex])
{
if (myWord[startindex] != 'a' && myWord[startindex] != 'e' && myWord[startindex] != 'i' && myWord[startindex] != 'o' && myWord[startindex] != 'u' )
{
pandi = 0;
}
else pandi = 1;
return pandi + CountVowels(myWord, startindex + 1);
}
return 0;
}
bool Sentence :: isPal(string myWord, int size)
{
int r = myWord.size() - size;
int t = size - 1;
if (size == r || r == t)
return true ;
if ((myWord[r]) != (myWord[t]))
return false ;
return isPal(myWord, -- size);
}
void Sentence :: Print()
{
cout << s [-- length];
if (length == 0)
{
cout << endl;
return ;
}
Print ();
}
/*Sentence :: ~Sentence()
{
cout << "\ntilde delete\n\n";
}*/
int main ()
{
string userW;
cout << "Enter a sentence: \n" ;
getline(cin, userW);
Sentence userSent(userW);
cout << "The number of vowels in the sentence is " << userSent.numVowel << endl;
cout << "" << endl;
cout << "The sentence " << userSent.s << " is" <<
(userSent.isPal(userSent.s, userSent.s.size()) ? " Palindrome\n" : " Not Palindrome\n" );
return 0;
}
Nov 9, 2015 at 12:56am UTC
Hey, can you provide some words that are palindromes that your program says aren't?
When I run this with the little gear in the corner, it works for all the words that I give it.
Nov 9, 2015 at 1:38pm UTC
Your code looks far to complicated for such a simple task. Is recursion required for some reason?. If not I got an easier solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include<iostream>
#include <cmath>
#include <string>
using namespace std;
struct Sentence
{
int CountVowels(string);
Sentence (string);
bool isPal(string , int );
void Print();
string s;
int numVowel;
int length;
bool IsVowel(char );
};
bool Sentence::IsVowel(char ch)
{
string vowels("aeiouAEIOU" );
return vowels.find(ch) != string::npos;
}
Sentence :: Sentence (string b)
{
s = b;
length = 0;
numVowel = CountVowels(s);
}
int Sentence :: CountVowels(string myWord)
{
string::size_type len = myWord.length();
int count = 0;
for (int i = 0; i < len; i++)
{
if (IsVowel(myWord[i]))
count++;
}
return count;
}
bool Sentence :: isPal(string myWord, int size)
{
string rev(myWord.rbegin(), myWord.rend());
return stricmp(rev.c_str(), myWord.c_str()) == 0;
}
void Sentence :: Print()
{
string rev(s.rbegin(), s.rend());
cout << s << endl;
}
int main ()
{
string userW;
cout << "Enter a sentence: \n" ;
getline(cin, userW);
Sentence userSent(userW);
cout << "The number of vowels in the sentence is " << userSent.numVowel << endl;
cout << "" << endl;
cout << "The sentence " << userSent.s << " is" <<
(userSent.isPal(userSent.s, userSent.s.size()) ? " Palindrome\n" : " Not Palindrome\n" );
system("pause" );
return 0;
}
Topic archived. No new replies allowed.