0

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).

2 Answers 2

3

Problem: in this code,

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

the

string *s1 = new string[len+numCons];

allocates an array of strings, instead of creating a longer string.

Instead do something like this:

string s1( len+numCons );

Yes it’s that simple.


In other news:

  • It's not a good idea to use the name i for anything other than a loop counter, since there's a very strong convention using it for that. And by convention i indicates integer. This stems from old Fortran, which got it from mathematics.

  • Comparision with true is unnecessary and in general an ungood idea (because some C functions return numbers other than 1 to indicate logical true). Consider “I have a house” versus “it’s true that I have a house” versus “it’s true that it’s true that I have a house”, and so on: these comparisons don’t contribute anything useful to the meaning.

  • Instead of pre-determining the relevant size and then creating a string of that size, you could simply start with an empty result string and use push_back or just += to add characters to the end.

Sign up to request clarification or add additional context in comments.

Comments

2
return *s1;

Compiler would treat this as *(s1+0) i.e only first element.

You should return the pointer.

return s1;

Accordingly change the signature of function to

string* doubleCons(string i) {

2 Comments

He'll also have to loop through the array and print.
You're missing the point of the code. The OP is not trying to return an array of strings (in which case the practical solution would be to use a std::vector). The OP is trying to return a longer string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.