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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include <iostream>
#include <cmath>
#include <algorithm>
using std::cout;
using std::cin;
using std::endl;
using std::string;
struct Words
{
int CountVowels(string, int);
public:
Words(string);
bool isPal(string, int);
void Print();
string Skippy;
int numVowel;
int length;
~Words();
};
Words::Words (string m)
{
Skippy = m;
length = 0;
numVowel = CountVowels(Skippy, 0);
}
int Words::CountVowels(string myWord, int startindex)
{
length++;
int pandi;
std::transform(myWord.begin(), myWord.end(), myWord.begin(), tolower);
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 Words::isPal(string myWord, int size)
{
int r = myWord.size() - size;
int t = size - 1;
std::transform(myWord.begin(), myWord.end(), myWord.begin(), tolower);
if (size == r || r == t)
return true;
//size = r is true when it is an even size string and
//the 2 middle characters have been checked
//r = t is true when it is an odd size string and the
//two chars on either side of the middle char have been checked
if ((myWord[r]) != (myWord[t]))
return false;
return isPal(myWord, --size);
}
void Words::Print()
{
cout << Skippy[--length];
if (length == 0)
{
cout <<endl;
return;
}
Print();
}
Words::~Words()
{
cout << "\ntilde delete\n\n";
}
long pows_n(long val, int exp)
{
if (exp == 0)
return 1;
return val * pows_n(val, --exp);
}
void BackwardNum(long num, bool init = true)
{
if (log10(num) > 0)
{
int t = num/10;
int k = t * 10;
cout << (num - k);
num = t;
BackwardNum(num, init = false);
}
else
{
if (num > 0 || init)
cout << num;
cout << endl;
}
}
int main()
{
string test;
long Val;
cout << "\nEnter a string: ";
getline(cin, test);
Words myTestWord(test);
cout << "The string " << myTestWord.Skippy << " is " <<
(myTestWord.isPal(myTestWord.Skippy, myTestWord.Skippy.size()) ? "palindrome\n" : "not palindrome\n");
cout << "The number of vowels in the string is " << myTestWord.numVowel << endl;
cout <<"String backwards is ";
myTestWord.Print();
cout << "\nEnter a number: ";
cin >> Val;
cout << Val << " to the power of 2 is " << pows_n(Val, 2);
cout << "\nNumber backwards is ";
BackwardNum(Val);
return 0;
}
|