Recursive function for "vowels",Recursive function for "sum of array"

Hello,

Will you please write a c++ program which includes all functions,

1) Recursive function for "vowels", that returns number of vowels in string.

2) Recursive function for "sum of array"

3) Program that uses a recursive function to check whether a string is Palindrome

4) Program that uses recursive function to print string backward

5) write a Recursive function for "backword number"(i.e: reversedigits, that takes an integer as a parameter and returns the number with digits)

6) Recursive function for "power"

Please let me know if i am not clear.

Thank you so much


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;
}




Enter a string: What is yours sruoy si tAHW
The string What is yours sruoy si tAHW is palindrome
The number of vowels in the string is 8
String backwards is WHAt is yours sruoy si tahW

Enter a number: 2563
2563 to the power of 2 is 6568969
Number backwards is 3652

tilde delete
Last edited on
@Smac89


Your code is invalid. For example function BackwardNum does not return a reverse number. And I think that it shall be declared as

long BackwardNum( long );

instead of

void BackwardNum(long num, bool init = true)

that is without any additional parameter except the parameter that will get the original number.
Last edited on
I see, here is one that works well

1
2
3
4
5
6
7
long BackwardNum(long num)
{
  int t = log10(num);
  if (t == 0)
    return num;
  return (((num%10) * pows_n(10, t)) + BackwardNum(num/10));
}
Topic archived. No new replies allowed.