Kindly assist me

Hello Guyz!
I Am Begginer and a desperate learner of c++
Am Habitual of Solving different problems to Learn more and more!

//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


I Am facing problem in the vowel part unable to find the syntax for it
kindly assist me
(note: this is not my homework just a problem from a c++ forum)
Guess what I found on the c++ website ?

http://www.cplusplus.com/reference/string/string/find_first_of/

Look at the example they have at the bottom
Try 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;
using std::getline;

int main(){

string
        firstName,
        lastName,
        fullName;

cout<<"Type your first name please: ";
getline(cin,firstName);
cout<<"Now your last name: ";
getline(cin,lastName);

fullName=firstName+" "+lastName;

cout<<"Hello "<<fullName<<endl;

//loop through firstName
for (unsigned character=0;character<firstName.length();character++){ 
         if(firstName[character]=='A'||firstName[character]=='a' //searching-> "Aa,Ee,Ii,Oo,Uu"
             ||firstName[character]=='E'||firstName[character]=='e'
             ||firstName[character]=='I'||firstName[character]=='i'
             ||firstName[character]=='O'||firstName[character]=='o'
             ||firstName[character]=='U'||firstName[character]=='u'){
                firstName[character]='z'; //and replace with z
        }//end if
  }//end loop for

//loop through lastName
for (unsigned character=0;character<lastName.length();character++){ 
         if(lastName[character]=='A'||lastName[character]=='a' //searching-> "Aa,Ee,Ii,Oo,Uu"
             ||lastName[character]=='E'||lastName[character]=='e'
             ||lastName[character]=='I'||lastName[character]=='i'
             ||lastName[character]=='O'||lastName[character]=='o'
             ||lastName[character]=='U'||lastName[character]=='u'){
                lastName[character]='z'; //and replace with z
        }//end if
  }//end loop for

//Now your new full name
fullName=firstName+" "+lastName;

cout<<"Hello "<<fullName<<endl;

//if you are on Windows You need a statement to simulate a pause e.j "cin.ignore();" here

return 0; //indicate successful termination
}//end main
@ eyenrique

A useful trick when it comes to checking characters and you don't want to check both the upper and lower case variants is to do use this simple bitwise operation to change the character to lowercase and read. Observe:

1
2
3
4
5
6
7
8
//loop through firstName
   for (unsigned character=0;character<firstName.length();character++){ 
      if(firstName[character] | 0x20 == 'a' || firstName[character] | 0x20 =='e' ||
	 firstName[character] | 0x20 =='i' || firstName[character] | 0x20 =='o' ||
	 firstName[character] | 0x20 =='u' ){
	 firstName[character] ='Z'|0x20;
      }//end if
   }//end loop for 


Using built in functions further cleans up the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
   #include <algorithm>

   char dipthong[] = { 'a', 'e', 'i', 'o', 'u' };
   char *ptr;
   
   //loop through firstName
   for (unsigned character=0;character<firstName.length();character++)
   { 
      if (( ptr = std::find(dipthong, dipthong+5, (firstName[character] | 0x20)) ) != dipthong+5 )
      {
	 *ptr = ('Z'|0x20);
      }
   }//end loop for 


Note if character is already lowercase, it will remain lowercase even after the operation
Last edited on
@ Smac89

Thanks for the advice.
Topic archived. No new replies allowed.