Feedback for my solution for a C++ excersice

Hallo everybody,
Few months ago I had a test in C++ and one of the assignment I was asked to do is the one I wrote below. I failed this assignment, but did not get any feedback, what was wrong with my solution. If you have the time and the will to help, I will appreciate it if you could tell me what is wrong with my solution. Thank you.

The assignment:
---------------------
class PiClass
{
public:
getCount(...){}
replacePiWithPI(...){}
};
int main()
{
const char *chText = "Pi pI PI pI Pi";
const wchar_t *wchText = L"Pi pI PI pI Pi";
// Invoke getCount(...) of class PiClass
// Invoke replacePiWithPI(...) of class PiClass
// Display on screen: "Found X occurrences of Pi. New string: Y"
}

Assifnment description:
1. Implement the functions getCount and replacePiWithPI of the class PiClass:
- getCount should return the number of occurrences of "Pi" within chText/wchText (case sensitive)
- replacePiWithPI should replace all occurrences of "Pi" in chText/wchText with "PI" (case sensitive)
2. Invoke the two functions getCount and replacePiWithPI.
3. Display the string given in the last comment on screen. X and Y should be replaced with the real values.
4. The class PiClass should be able to deal with both chText (ASCII) and wchText (Unicode).


This is my solution for the assignment:
---------------------------------------

#include <iostream>
using namespace std;

template<typename T>
class PiHelper
{
private:
static bool isSizeSmallerThan(const T* str, int n) {
if (!str) return true;
for (int i=0; i<n; ++i) {
if (str[i] == 0) return true;
}
return false;
}
static const T* findNextMatch(const T* str, const T* substr, int n)
{
if (!str) return true;
for (; str[n-1] != 0; ++str) {
if(!memcmp(str, substr, n*sizeof(T))) {
return str;
}
}
return NULL;
}
public:
static int getCount(const T* str, const T* substr, int n)
{
if(isSizeSmallerThan(str,n)) return 0;
int counter = 0;
while ((str = const_cast<T*>(findNextMatch(str, substr, n)))) {
++counter;
str += n;
}
return counter;
}
static void replaceWith(T* str, const T* substr, const T* replace, int n)
{
if(isSizeSmallerThan(str,n)) return;
while ((str = const_cast<T*>(findNextMatch(str, substr, n)))) {
memcpy(str, replace, n*sizeof(T));
str += n;
}
}
};

class PiClass
{
public:
int getCount(const char* str) {
return PiHelper<char>::getCount(str, "Pi", 2);
}
int getCount(const wchar_t* str) {
return PiHelper<wchar_t>::getCount(str, L"Pi", 2);
}
void replacePiWithPI(char* str) {
return PiHelper<char>::replaceWith(str, "Pi", "PI", 2);
}
void replacePiWithPI(wchar_t* str) {
return PiHelper<wchar_t>::replaceWith(str, L"Pi", L"PI", 2);
}
};

int main()
{
const char *chText = "Pi pI PI pI";
const wchar_t *wchText = L"Pi pI PI pI";

char *chNewText = strdup(chText);
wchar_t *wchNewText = wcsdup(wchText);

PiClass algorithm;
int count = 0;

count = algorithm.getCount(chNewText);
algorithm.replacePiWithPI(chNewText);
cout << "Found " << count << " occurrences of Pi. New string: "
<< (chNewText ? chNewText : "" )<< endl;

count = algorithm.getCount(wchNewText);
algorithm.replacePiWithPI(wchNewText);
wcout << L"Found " << count << L" occurrences of Pi. New string: "
<< (wchNewText ? wchNewText : L"" ) << endl;

free(chNewText);
free(wchNewText);
}
Last edited on
Topic archived. No new replies allowed.