如何让我的代码处理wchar_t

时间:2012-12-11 15:06:15

标签: wchar

我真的希望得到一些帮助。 我对C ++编程比较陌生。我需要我的代码才能处理ASCII和Unicode。以下是输入szTestString1,在输入中找到“Ni”并用“NI”替换它。 szTestString1是ASCII,所以我的代码工作正常。如何使代码能够处理szTestString2? 我用wstring替换了字符串,用L“Ni”替换了“Ni”。此外,我使用wcout而不是cout,但输出没有多大意义。我现在迷路了。 任何提示都将不胜感激。

#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;



}

0 个答案:

没有答案