C++ Template:
class MyClass
{
public:
getNiCount(...)
{
}
replaceNiWithNI(...)
{
}
};
int main()
{
const char *szTestString1 = "Ni nI NI nI Ni";
const wchar_t *szTestString2 = L"Ni nI NI nI Ni";
// Invoke getNiCount(...) of class MyClass
// Invoke replaceNiWithNI(...) of class MyClass
// Display on screen: "Found X occurrences of Ni. New string: Y"
}
Task description:
- Implement the two functions
getNiCountandreplaceNiWithNIof the classMyClass:getNiCountshould return the number of occurrences of "Ni" withinszTestString1/2(case sensitive)replaceNiWithNIshould replace all occurrences of "Ni" inszTestString1/2with "NI" (case sensitive)
- Invoke the two functions
getNiCountandreplaceNiWithNI. - Display the string given in the last comment on screen.
XandYshould be replaced with the real values. - The class
MyClassshould be able to deal with bothszTestString1(ASCII) andszTestString2(Unicode).
General requirements:
The code should be
- easy to understand and maintain (Priority 1)
- technically elegant (Priority 2)
- as (CPU) efficient as possible (Priority 3)
You’re allowed to use all techniques, toolkits and frameworks which are based on the C++ language.
my solution (incomplete)
The logic is below... However in my system the function2 "replace" is crashing. Cannot get it fixed.
#include<iostream>
#include<string>
using namespace std;
class MyClass
{
public:
void getNiCount(const char*,const wchar_t*);
//cout<<"\nCount is :"<<count;
void replaceNiWithNI(const char*,const wchar_t*);
};
void MyClass::getNiCount(const char* x,const wchar_t* y)
{
int count=0;
int ycount=0;
for(int i=0; x[i]!='\0';i++)
{
if(x[i]=='N')
{ if(x[i+1]=='i')
count++;
}
}
for(int i=0; y[i]!='\0';i++)
{
if(y[i]=='N')
{ if(y[i+1]=='i')
ycount++;
}
}
cout<<"\nFound "<<count<<" occurences of Ni in String 1";
cout<<"\nFound "<<ycount<<" occurences of Ni in String 2";
}
void MyClass:: replaceNiWithNI(const char* x,const wchar_t* y)
{ char* a;
wchar_t* b;
strcpy(a,x);
for (int i=0;a[i]!='\0';i++)
{
if (a[i]=='N')
{ if(a[i+1]=='i')
{
a[i+1]='I';
}
}
}
for (int i=0;y[i]!='\0';i++)
{
b[i]=y[i];
}
for (int i=0;b[i]!='\0';i++)
{
if (b[i]=='N')
{ if(b[i+1]=='i')
{
b[i+1]='I';
}
}
}
cout<<"\nNew String 1 is :";
puts(a);
cout<<"\nNew String 2 is :";<<b
}
int main()
{
const char *szTestString1 = "Ni nI NI nI Ni";
const wchar_t *szTestString2 = L"Ni nI NI nI Ni";
MyClass ob1;
ob1.getNiCount(szTestString1,szTestString2);
ob1.replaceNiWithNI(szTestString1,szTestString2);
getchar();
return 0;
}