My code is supposed to clear any character that isn't a-z or A-Z. For other characters, for instance á à ã ă â é è ê if I can make it work, I'll make them change from á to a and è to e etc.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int counter=0;
string* word = new string[1];
string b ="áhelloá";//nothing
word[0] = "áapple_.Dogá.";//doesnt work
//word[0] = "apple_.Dog.";//if there is no characters like á it works
cout<<endl<<word[0].length()<<endl;
for (int i = 0; i < word[0].length(); ++i)
{
if(word[0][i] >= 'A' && word[0][i] <='Z' || word[0][i] >= 'a' && word[0][i] <='z')
{
cout<<"Current: "<<word[0][i]<<endl;//shows what characters passed if
}
else
{
cout<<"Erased: "<<word[0][i]<<endl;//shows what was erased
word[0].erase(i,1);//deletes char
i--;
}
}
cout<<endl<<word[0];//prints final word,after erase
return 0;
}
If I run my code with for example á in Clion it doesn't do anything and returns 0. I tested the same on Repl.it and it somewhat works as intended, I think. Is there a problem with my Clion? What am I doing wrong?
You can use
wcoutandwstringto deal with Unicode character in C++ within Windows:Result :
For the question "Why it works on repl.it?":
It should be noted that different compiler and platform handles Unicode character very differently. Quoting @bames53 :
By the way, IMO you're using dynamic array for no reason. A
wstringis enough.Also see
Why is "using namespace std;" considered bad practice?
How to print Unicode character in C++?