Changing vowels to 'z'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"


#include <iostream>
#include <string>
int main(){
	std::cout << "First Name?";
	std::string firstName;
	std::cin >> firstName;
	
	std::cout << "Last Name?";
	std::string lastName;
	std::cin >> lastName;
	std::string fullName;
	fullName = firstName + " " + lastName;
	char n;
	for (n = 0; n < fullName.length(); n++);
	if (fullName[n] == 'a' || fullName[n] == 'e' || fullName[n] == 'i' || fullName[n] == 'o' || fullName[n] == 'u'){
		fullName[n] = 'z';
	}
	std::cout << fullName;
}

It doesn't change vowels to 'z', please help.
Last edited on
Remove the semicolon at the end of line 17.
^ will do what you want.

Also, for readability and good programming practices, you should include brackets around the if (it is optional in this case, but it will make this better.

The output is also kind of hard to read. You should put ' ' or '\n' after each output, and it will look better with a std::cout << fullName << std::endl; on line 21.

Finally, this program would benefit from a switch case. http://www.cplusplus.com/doc/tutorial/control/ scroll all the way to the bottom. Switch case has fall-through execution (that's not the correct term, but you'll get the point) which means that unless you break out, it will continue executing until it hits the end of the switch block. That means you can rewrite the program with a switch case using fall-throughs for each vowel.

Hope this helps.
There is also std::string::find_first_of. Documentation is on this site.
I would do it like this:
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
#include <iostream>
#include <string>
#include <cstring>

bool IsVowel (char& ch);

int main ()
{
  std::cout << "First Name?";
  std::string firstName;
  std::cin >> firstName;

  std::cout << "Last Name?";
  std::string lastName;
  std::cin >> lastName;
  std::string fullName;
  fullName = firstName + " " + lastName;
  char n;
  
  for (char& ch : fullName)
  {
    if (IsVowel (ch))
      ch = 'z';
  }

  std::cout << '\n' << fullName << '\n'; 
  
  system ("pause");
}

bool IsVowel (char& ch)
{
  const char *vowels = "aeiouAEIOU";

  return strchr (vowels, ch) != 0;
}

Another pretty simple way to do this program. If you can't use C++11, then just use your for loop and replace ch with fullName[n] like you had it in your loop.

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
#include "stdafx.h"


#include <iostream>
#include <string>
int main()
{
	std::cout << "First Name? ";
	std::string firstName;
	std::cin >> firstName;
	
	std::cout << "Last Name? ";
	std::string lastName;
	std::cin >> lastName;
	std::string fullName;

	fullName = firstName + " " + lastName;

	for (char &ch : fullName)
	{
		switch(tolower(ch))
		{
		case 'a':
		case 'e':
		case 'i':
		case 'o':
		case 'u':
			ch = 'z';
		}
	}
	std::cout << fullName << std::endl;
}


You could also do a replaceVowels function in place of the for loop if you want to make it more reactive.
Last edited on
Topic archived. No new replies allowed.