How would you do this exercise?

So I'm almost done with all the beginner exercises in this link
http://www.cplusplus.com/forum/articles/12974/ (Now is time to move on to the graduation exercise) and I just wanted to ask how would you guys solve this exercise.

task: Write a program that asks for a user first name and last name separately.
The program must then store the users full name inside a single string and out put it to the string.
i.e.
Input:
John
Smith
Output:
John Smith

★ Modify the program so that it then replaces every a, e, i , o, u w/ the letter z.
i.e.
John Smith -> Jzhn Smzth

★★ Modify the Program so that it reverses the users name
i.e.
John Smith -> htimS nhoJ

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

using namespace std;

void enterName(string &);
string replaceVowel(string &);
void reverseName(string &);

int main()
{
    string fullName;
    enterName(fullName);
    cout << endl;
    cout << replaceVowel(fullName);
    cout << endl << endl;
    reverseName(fullName);
    cout << endl;
    return 0;
}

void enterName(string &fullName)
{
    string firstName, LastName;

    cout << "Please enter your first name. ";
    getline(cin, firstName, '\n');

    cout << "Please enter your last Name. ";
    getline(cin, LastName, '\n');

    fullName = firstName + " " + LastName;

    cout << "Hello, " << fullName << endl;
}

string replaceVowel(string &fullName)
{
    for (unsigned int i = 0; i < fullName.length(); i++)
    {
        if (toupper(fullName[i]) =='A' || toupper(fullName[i]) == 'E' ||
            toupper(fullName[i]) =='I' || toupper(fullName[i]) == 'O' ||
            toupper(fullName[i]) == 'U')
        {
            fullName[i] = 'z';
        }
    }

    return fullName;
}

void reverseName(string &fullName)
{
    for(string::reverse_iterator rit = fullName.rbegin(); rit != fullName.rend(); ++rit)
    {
        cout << *rit;
    }
}
Last edited on
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
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

std::string get_full_name()
{
    std::cout << "Enter your first name: ";
    std::string first;
    std::getline(std::cin >> std::ws, first);
    std::cout << "Enter your last name: ";
    std::string last;
    std::getline(std::cin >> std::ws, last);
    return first += last;
}

bool is_vowel(char c)
{
    static const std::string vowels = "AEIOU";
    return (vowels.find(::toupper(c)) != std::string::npos);
}

std::string mangle(std::string s)
{
    std::replace_if(s.begin(),s.end(), is_vowel, 'z');
    return s;
}

std::string reverse(std::string s)
{
    std::reverse(s.begin(), s.end());
    return s;
}

int main()
{
    std::string name = get_full_name();
    std::cout << "Hello, " << name << '\n';
    std::cout <<  mangle(name)     << '\n';
    std::cout << reverse(name)     << '\n';
}
Last edited on
Thanks Miniipaa
Hey MiiniPaa, I've always seen you use cin >> std::ws. What is the ws for?
Last edited on
std::ws is an input manipulator which consumes whitespaces. It is useful when you are not sure if tere is whitespace characters (especially newline which tends to break getline) left by formatted input and your string should not have leading whitespaces anyway.

Actually I was lazy and did not remove trailing spaces from string after reading, which is a good idea to do.
Topic archived. No new replies allowed.