Help. Strings already betrayed me.

Hello, I'm trying to solve the excersice "Strings are your friends, untill they betray you" from http://www.cplusplus.com/forum/articles/12974/
I know that the code is not the most efficient, as i could use the function find_first_of, but I haven't read about other libraries yet, and im trying yo solve it using the default.

I'm new to pointers and can't seem to fully understand them. I'm getting an error when I try to call the function replaceVowels by reference so I can print it out in main().

The code worked out fine when it was all written in main() with no function calls, but I wanted to try it out.



Also if you could please help me sort out how to make the compiler to print out a reverse string that would be grat. Thank you guys.



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
  #include <iostream>
#include <string>

using namespace std;

void replaceVowels(string str);

int main()
{
    string firstname;
    string lastname;
    string fullname;

    cout << "Name: ";
    cin >> firstname;

    cout << "Lastname: ";
    cin >> lastname;

    fullname = firstname +" "+ lastname;


   replaceVowels(&fullname);


    cout << "Your name is "<<fullname <<". "<<endl;

 return 0;
}

void replaceVowels(string* str, char replacedLetter='z')
{
     for (int i=0; i<str.length();i++)
    {
        if (str[i]== 'a')
        {
           str[i] = replacedLetter;
        }
        else if (str[i]== 'e')
        {
           str[i] = replacedLetter;
        }
        else if (str[i]== 'i')
        {
           str[i] = replacedLetter;
        }
        else if (str[i]== 'o')
        {
           str[i] = replacedLetter;
        }
        else if (str[i]== 'u')
        {
           str[i] = replacedLetter;
        }
    }
}

Last edited on
Why not just pass it as a string by reference like this:

1
2
3
4
5
6
void replaceVowels(string& str, char replaceLetter)
{

   // replace all vowels with 'z';

}


Function call would look like this: replaceVowels(fullname);

By the way, for functions with default parameters, the prototype should have the default value, and the definition should be made without them.

So prototype: void replaceVowels(string& str, char replacedLetter='z');
Function definition: void replaceVowels(string& str, char replacedLetter)

Last edited on
I tried it and it doesnt work.

C:\Excerise Array\main.cpp|23|undefined reference to `replaceVowels(std::string)'|
Worked for me. Maybe you did something wrong. Here is the full code that Im talking about:

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
#include <iostream>
#include <string>

using namespace std; 

void replaceVowels(string& str, char replacedLetter='z'); // prototype has default value

int main ()
{
    string firstname;
    string lastname;
    string fullname;

    cout << "Name: ";
    cin >> firstname;

    cout << "Lastname: ";
    cin >> lastname;

    fullname = firstname +" "+ lastname;


    replaceVowels(fullname); // notice, no need for & operator in function call


    cout << "Your name is "<<fullname <<". "<<endl;

    return 0;
}

// string passed by reference, char has default value 'z' (see prototype above)
void replaceVowels(string& str, char replacedLetter) 
{
     for (int i=0; i<str.length();i++)
    {
        if (str[i]== 'a')
        {
           str[i] = replacedLetter;
        }
        else if (str[i]== 'e')
        {
           str[i] = replacedLetter;
        }
        else if (str[i]== 'i')
        {
           str[i] = replacedLetter;
        }
        else if (str[i]== 'o')
        {
           str[i] = replacedLetter;
        }
        else if (str[i]== 'u')
        {
           str[i] = replacedLetter;
        }
    }
}



Name: Arslan
Lastname: Sana
Your name is Arslzn Sznz.

Process returned 0 (0x0)   execution time : 7.862 s
Press any key to continue.


Notice, however, that only the lowercase vowels were changed to 'z'. You can fix this by also checking for uppercase vowels in the if-conditions.


Last edited on
Also if you could please help me sort out how to make the compiler to print out a reverse string that would be grat.


To print a string in reverse, simply loop through it in reverse order and print every character.
Thank you very much, i have to practice more to fully understand how pointers and passing by reference work.
Topic archived. No new replies allowed.