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;
}
There are a few issues here:
Your program fails to compile because of the misplaced semi-colon in
Your program crashes at
strcpy(a,x);because you’re copying intoawhich is uninitialised – it has no memory allocated. You’d need to callnewonafor this to work, which would also mean you’d need to know the size of the array required (another parameter for the function probably).Using
std::stringandstd::wstringis almost always preferable to dealing with raw char arrays. See this question for example. I see you’d probably already considered it since you’ve got#include <string>Since you’re required to perform identical operations on differing types, I suspect the point of the exercise might have been to use templates.
You said
yet your
getNiCountdoesn’t return anything.using namespace std;is often considered bad practice.It’s generally worth favouring pre-increments rather than post-increments, although in this particular case, there’s no overhead.
To give you an example including the recommendations above:
I’ve generally left the way you locate and replace the
Nichars as you had it. There are more sophisticated options available in the member functions ofstd::stringand in<algorithm>library.