string (theString)

okay so I know that my consonant and vowel part is wrong but I attempted change char to string so that I may relay this back out the the user. I am also having trouble with the reiterator, can someone please help me?

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
 #include <iostream>
#include <string>
#include <cctype>
using namespace std;

const int MINCHARINPUT = 4; //GLOBAL DEFINED CONSTANT FOR MINIMUM CHARACTER INPUT
const char vowels[5] = {'a','e','i','o','u'}; //GLOBAL DEFINED ARRAY OF 5 CONTAINING LOWERCASE VOWELS


//Function prototypes
string getString(); // function accepting no variables that returns a string
string convertToUpper(string theString); // function that accepts a string and returns a string
string convertToLower(string theString);
int countVowels(string theString);
int countConsonants(string theString);
string reverseStr(string theString);

int main()
{
   //Initialize variables
   string theString;
   theString = getString();
   cout << "Input String: " <<theString <<endl;
   string tmep = convertToUpper(theString);
   cout << "Input String Converted to Upper case: " << tmep << endl;
   string tnep = convertToLower(theString);
   cout << "Input String Converted to Lower case: " << tnep << endl;
   int tvep = countVowels(theString);
   cout << "Number of Vowels in the Input String: " << tvep << endl;
   int tcep = countConsonants(theString);
   cout << "Number of Consonants in the Input String: " << tcep << endl;
   string trep = reverseStr(theString);
   cout << "Input statement Reveresed: " << trep << endl;
   return 0;
}

string getString()
{
   string input;

   cout << "Enter a string with 4 or more characters: " << endl;
   getline(cin, input);
   while (input.length() < MINCHARINPUT)
   {
      cout << "Invalid string! Please try again. " << endl;
      cout << "Input-> ";
      getline(cin, input);
   }
   return input;
}

string convertToUpper(string theString)
{
   string uppercaseStr;
   uppercaseStr = theString;
   for (int i = 0; i < uppercaseStr.length(); i++)
   {
      uppercaseStr[i] = toupper(theString[i]);
   }
   return uppercaseStr;

}

string convertToLower(string theString)
{
   string lowercaseStr;
   lowercaseStr = theString;
   for (int i = 0; i < lowercaseStr.length(); i++)
   {
      lowercaseStr[i] = tolower(theString[i]);
   }
   return lowercaseStr;

}

int countVowels (string theString)
{
   char vowels;
   for (int i = 0; i < vowels; i++)
   {
      if (vowels == 'a' || vowels == 'e' ||
          vowels == 'i' || vowels == 'o' ||
          vowels == 'u')

         vowels = vowels +1;
   }

   return vowels;
}

int countConsonants (string theString)
{
   char consonants;
   for (i = 0; i <consonants; i++)
   {
      if (consonants != vowels)
         consonants= consonants+1;
   }
   return consonants;
}

string reverseStr (string theString)
{
   string reverse;
   reverse = theString;
   char rit,str;
   for (string reverse_iterator; rit=str.rbegin(); rit!=str.rend(); ++rit)
   {
      cout << *rit;
   }
   return 0;
}
First of all, it's always a good idea to search up info about things your not sure of:
http://www.cplusplus.com/reference/iterator/reverse_iterator/reverse_iterator/

So string iterators are declared like this: string::iterator you can set it to exampleString.begin() or exampleString.end() because they return a string iterator. char type just can't do that using string::iterator so here is the correct function:

1
2
3
4
5
6
7
string reverseStr(string theString)
{
   string reversedstring;
   for (string::reverse_iterator iter = theString.rbegin(); iter != theString.rend(); iter++)
      reversedstring += *iter;
   return reversedstring;
}
Topic archived. No new replies allowed.