Thursday, October 30, 2014

Delete Vowels -- Amazon

#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<unordered_set>
#include<time.h>
#include<stdio.h>

using namespace std;

bool checkVowel (char c){
if(c>='A' && c<='Z'){
c = c - 'A' +'a';
}
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u'){
return true;
}
return false;
}
/*
void removeVowels(string &ori){
int len = ori.length();
if(len ==0)return;

int count = 0;
for(int i=0; i< len;++i){
if(checkVowel(ori[i]) == true){        //if ori[i] is a vowal character, 'count' slots ++, which should be deleted.
            count++;
}
else{
ori[i-count] = ori[i];             //left shift accumulated 'count' slots for an non-vowel character, modify ori string directly.
}
}
ori[len-count] = '\0';
for(int i= len-count;i< len;++i){
ori[i] = NULL;
}
}
*/
string removeVowels(string &ori){
string res = "";
int len = ori.length();
if(len ==0)return res;

for(int i=0;i<len;++i){
   if(checkVowel(ori[i])== false){         //if ori[i] is not a vowel character, insert it to res string's tail.
res.insert(res.end(),ori[i]);
}
}
return res;
}


int main(){
string original = "AbcdeFghijKiou123";
cout<<"Before : "<< original << endl;
string after = removeVowels(original);
cout<<"After : "<< after << endl;
getchar();
return 0;
}




No comments:

Post a Comment