I wrote a function in C that receives a pointer to a character array as argument and switch all accented letters with their corresponding character, the code is as follows:
void removeacc(char *texto){
char a[]="\x83\x84\x85\x86\xa0\xc6",A[]="\x8e\x8f\xb5\xb6\xb7\xc7";
char e[]="\x88\x89\x8a\x82",E[]="\x90\xd2\xd3\xd4";
char i[]="\x8b\x8c\x8d\xa1",I[]="\xd6\xd7\xd8\xde";
char o[]="\x93\x94\x95\xa2\xe4",O[]="\x99\xe0\xe2\xe3\xe5";
char u[]="\x81\x96\x97\xa3",U[]="\x9a\xe9\xea\xeb";
char n[]="\xa4",N[]="\xa5";
char c[]="\x87",C[]="\x80";
char y[]="\x98\xec",Y[]="\xed";
int x,z;
for(x = 0; x < strlen(texto); x++){
for(z = 0; z < 7; z++){
if(texto[x] == a[z]) texto[x] = 'a';
if(texto[x] == A[z]) texto[x] = 'A';
}
for(z = 0; z < 6; z++){
if(texto[x] == o[z]) texto[x] = 'o';
if(texto[x] == O[z]) texto[x] = 'O';
}
for(z = 0; z < 5; z++){
if(texto[x] == e[z]) texto[x] = 'e';
if(texto[x] == E[z]) texto[x] = 'E';
if(texto[x] == i[z]) texto[x] = 'i';
if(texto[x] == I[z]) texto[x] = 'I';
if(texto[x] == u[z]) texto[x] = 'u';
if(texto[x] == U[z]) texto[x] = 'U';
}
if(texto[x] == n[0]) texto[x] = 'n';
if(texto[x] == N[0]) texto[x] = 'N';
if(texto[x] == c[0]) texto[x] = 'c';
if(texto[x] == C[0]) texto[x] = 'C';
if(texto[x] == y[0]) texto[x] = 'y';
if(texto[x] == y[1]) texto[x] = 'y';
if(texto[x] == Y[0]) texto[x] = 'Y';
}
}
As you can see it searches for escape characters corresponding to the accented letters and substitute. My brother looked at my code and said it was not polished enough and could be more efficient, he also stated that the following if's should be encapsulated as exeption from the previous.
- Would this really boost performance?
- And what changes you guys think could I do to make it better?
- Kinda off-topic but is there a method to read text from a file with the correct accented letters? fgets puts random letters instead of the accented letters :(