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