I would really like some help in this. I am relatively new to programming in C++. I need my code to be able to deal with ASCII and Unicode. The following is taking an input szTestString1, finds "Ni" in the input and replaces it with "NI". szTestString1 is ASCII so my code works fine. How can I make the code be able to handle szTestString2 as well? I substituted string with wstring and "Ni" with L"Ni". Furthermore I used wcout instead of cout, but the output did not make much sense. And I am now lost. Any hint would be greatly appreciated.
#include <iostream>
#include <string> using namespace std;
class MyClass {
public: int getNiCount(string s1) { int Ni_counter=0; int found,pos;
for (pos=0; found!=-1; pos+=(found+2)){ found=s1.find("Ni",pos);
Ni_counter++; }
return(Ni_counter-1); }
string replaceNiWithNI(string s1) { int found,pos;
do{ found=s1.find("Ni",pos); if (found!=-1) s1.replace(found, sizeof("Ni")-1, "NI"); }while (found!=-1); return(s1); }
} obj1;
int main()
{
const char *szTestString1 = "Ni ll Ni ll Ni nI NI nI Ni Ni"; const wchar_t *szTestString2 = L"Ni ll Ni ll Ni nI NI nI Ni Ni";
int Ni_occur_number; string new_string;
// Invoke getNiCount(...) of class MyClass Ni_occur_number=obj1.getNiCount(szTestString1); // Invoke replaceNiWithNI(...) of class MyClass new_string=obj1.replaceNiWithNI(szTestString1);
// Display on screen: "Found X occurrences of Ni. New string: Y" cout << "Found " << Ni_occur_number << " occurrences of Ni. " << "New string: " << new_string << endl;
}