I'm trying to create a function for school that returns an array where any consonants are doubled (ex. Hello = HHellllo). This is my code
#include <iostream>
#include <string>
using namespace std;
bool isConsonant(char c) {
if ((c >=97 && c <= 122 && c != 'a' && c!= 'e' && c!= 'i' && c!= 'o' && c != 'u') | (c >=65 && c <= 90 && c != 'A' && c!= 'E' && c!= 'I' && c!= 'O' && c != 'U')) {
return true;
}
else
return false;
}
string doubleCons(string i) {
int len = (int) i.length();
int numCons = 0;
for (int j = 0; j < len; j++) {
if (isConsonant(i[j]) == true) {
numCons++;
}
}
int s1count = 0;
string *s1 = new string[len+numCons];
for (int j = 0; j < (len); j++) {
if (isConsonant(i[j]) == true) {
s1[s1count] = i[j];
s1[s1count+1] = i[j];
s1count += 2;
}
else {
s1[s1count] = i[j];
s1count++;
}
}
return *s1;
}
int main() {
string s = "Hello";
s = doubleCons(s);
cout << s << endl;
}
Main is just a test to see if it works. The problem is, the output for main is just H, nothing else.
I've tried looping through s[i] and i just get H????. I can't figure out why the string isn't HHellllo or anything more than H for that matter. Can anybody identify the problem? I've tried debugging with cout statements and I am condiment that isConsonant works fine, as well as the first for loop (numCons = 3 which is correct for hello).