用C ++中的普通元音替换重音元音
Replace accentuated vowels with common vowels in c++
我有一个带有西班牙语文本的字符串,我想像这样替换某些字符:
character
replace-with:
á
a
é
e
í
i
ó
o
ú
u
ñ
n
我试过了,没有结果:
std::string RemoverTildes(std::string str){
std::string sinTildes = str.c_str();
for(int i = 0; i < str.length(); i++){
if(sinTildes[i] == 'á'){
sinTildes[i] = 'a';
} else if(sinTildes[i] == 'é'){
sinTildes[i] = 'e';
} else if(sinTildes[i] == 'í'){
sinTildes[i] = 'i';
} else if(sinTildes[i] == 'ó'){
sinTildes[i] = 'o';
} else if(sinTildes[i] == 'ú'){
sinTildes[i] = 'u';
} else if(sinTildes[i] == 'ñ'){
sinTildes[i] = 'n';
}
}
return sinTildes;
}
我怎样才能做到这一点?有没有我可以用来做这件事的图书馆或其他东西?
您可以使用 Unicode (UTF-16LE) wchar_t
来操作此字符。您可以阅读有关 char
类型 here.
的更多信息
这是您的代码和使用示例。
#include <iostream>
void tildesRemover(wchar_t* wideString) {
auto stringLen = sizeof(wideString);
for (int i = 0; i < stringLen; i++){
if (wideString[i] == L'á'){
wideString[i] = 'a';
} else if (wideString[i] == L'é'){
wideString[i] = 'e';
} else if (wideString[i] == L'í'){
wideString[i] = 'i';
} else if (wideString[i] == L'ó'){
wideString[i] = 'o';
} else if (wideString[i] == L'ú'){
wideString[i] = 'u';
} else if (wideString[i] == L'ñ'){
wideString[i] = 'n';
}
}
}
int main() {
wchar_t wideString[] = L"áíéíúñó";
tildesRemover(wideString);
std::wcout << wideString << std::endl;
return 0;
}
L
这里指定wchar_t
.
我有一个带有西班牙语文本的字符串,我想像这样替换某些字符:
character | replace-with: |
---|---|
á | a |
é | e |
í | i |
ó | o |
ú | u |
ñ | n |
我试过了,没有结果:
std::string RemoverTildes(std::string str){
std::string sinTildes = str.c_str();
for(int i = 0; i < str.length(); i++){
if(sinTildes[i] == 'á'){
sinTildes[i] = 'a';
} else if(sinTildes[i] == 'é'){
sinTildes[i] = 'e';
} else if(sinTildes[i] == 'í'){
sinTildes[i] = 'i';
} else if(sinTildes[i] == 'ó'){
sinTildes[i] = 'o';
} else if(sinTildes[i] == 'ú'){
sinTildes[i] = 'u';
} else if(sinTildes[i] == 'ñ'){
sinTildes[i] = 'n';
}
}
return sinTildes;
}
我怎样才能做到这一点?有没有我可以用来做这件事的图书馆或其他东西?
您可以使用 Unicode (UTF-16LE) wchar_t
来操作此字符。您可以阅读有关 char
类型 here.
这是您的代码和使用示例。
#include <iostream>
void tildesRemover(wchar_t* wideString) {
auto stringLen = sizeof(wideString);
for (int i = 0; i < stringLen; i++){
if (wideString[i] == L'á'){
wideString[i] = 'a';
} else if (wideString[i] == L'é'){
wideString[i] = 'e';
} else if (wideString[i] == L'í'){
wideString[i] = 'i';
} else if (wideString[i] == L'ó'){
wideString[i] = 'o';
} else if (wideString[i] == L'ú'){
wideString[i] = 'u';
} else if (wideString[i] == L'ñ'){
wideString[i] = 'n';
}
}
}
int main() {
wchar_t wideString[] = L"áíéíúñó";
tildesRemover(wideString);
std::wcout << wideString << std::endl;
return 0;
}
L
这里指定wchar_t
.