Pages

Saturday, June 2, 2012

739 - Soundex Indexing


//
// main.cpp
// Soundex Indexing
//
// Created by Panks on 24/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
using namespace std;



int getcode(char a);

int main (int argc, const char * argv[])
{

string input;
bool done=false;
int i;
string code, ans;
cout<<" NAME SOUNDEX CODE"<<endl;
while (cin>>input) {
done=false;
i=0;
code="";
while (i<input.length()&&!done) {
if(i>0&&(input[i]=='A'||input[i]=='E'||input[i]=='I'||input[i]=='O'||input[i]=='U'||input[i]=='Y'||input[i]=='W'||input[i]=='H'||getcode(input[i])==getcode(input[i-1]))){
i++;
continue;
}
if (code.length()==4) {
done=true;
continue;
}else if(code.length()>0&&code.length()<4){
code=code+char(getcode(input[i])+48);
i++;
}else{
code=input[i];
i++;
}
}
if (code.length()!=4) {
while (code.length()!=4) {
code=code+'0';
}
}
ans="";
ans=" "+input;
while (ans.length()!=34) {
ans=ans+' ';
}
ans=ans+code;
cout<<ans<<endl;
}
cout<<" END OF OUTPUT"<<endl;
return 0;
}

int getcode(char a){
if(a=='B'||a=='P'||a=='F'||a=='V'){
return 1;
}else if(a=='D'||a=='T'){
return 3;
}else if(a=='L'){
return 4;
}else if(a=='M'||a=='N'){
return 5;
}else if(a=='R'){
return 6;
}else if(a=='C'||a=='S'||a=='K'||a=='G'||a=='J'||a=='Q'||a=='X'||a=='Z'){
return 2;
}else{
return -1;
}
}

No comments:

Post a Comment