In: Computer Science
You are required to use C++ static or dynamic arrays of characters to store c-strings. You are NOT allowed to use any C++ string data type variable for any purpose. Moreover, you are allowed to add any include directive. You are not allowed to include string, cstdlib or math libraries. Also, you are not allowed to use any built-in functions of c-strings.
can someone help with the third, fourth, and fifth functions? I tried many ways but i cannot figure them out...i don't know how to search char array and arrangement of range. I need it urgently.
typedef char* charPointer;
int cstrlen(const charPointer& s)
{
int i = 0;
int count = 0;
while (s[i] != '\0')
{
count++;
i++;
}
return count;
}
int countChars(const charPointer& s, const char&
ch)
{
int count = 0;
cstrlen;
for (int i = 0;i < cstrlen(s); i++)
{
if (s[i] == ch)
{
count++;
}
}
return count;
}
int findChar(const charPointer& s, const char& ch, const
int& startIndex = 0, const int& lastIndexTemp = -1)
{
/*
returns the smallest index where the character ch is
found in s starting from
startIndex (inclusive) upto lastIndex (exclusive). The
default argument value
for startIndex is 0. The default argument value for
lastIndexTemp is -1 in which
case cstrlen(s) must be used instead of -1.
For example,
findChar("test", 't', 1, 4) must
return 3. Here startIndex = 1, lastIndex = 4
findChar("test", 't', 3) must
return 3. Here startIndex = 3, lastIndex = 4
findChar("test", 't', 1, 3) must
return -1. Here startIndex = 1, lastIndex = 3
findChar("test", 't') must return
0. Here startIndex = 0, lastIndex = 4
If ch is not found in s in the given interval, the the
function must return -1
This function must first validate both the startIndex
and lastIndex.
That is, if lastIndex > cstrlen(s) or startIndex
< 0 it must return -1
*/
}
charPointer getCopy(const charPointer& s)
{
/*
returns a new cstring that is a copy of the cstring
s.
That is a new cstring with as big memory as the size
of
the cstring s is created and then all the characters
of
s including the null char are copied to it.
*/
}
void rotateString(const charPointer& s, const int&
r)
{
/*
Rotates the characters of s by r units
If r > 0, rotate the characters
of s to the left
If r < 0, rotate the characters
of s to the right
Please note the value of r can be
any integer even larger than the length of s
For example,
"asmara" rotated
to the left by 0 becomes "asmara"
"asmara" rotated
to the left by 1 becomes "smaraa"
"asmara" rotated
to the left by 2 becomes "maraas"
"asmara" rotated
to the left by 3 becomes "araasm"
"asmara" rotated
to the left by 4 becomes "raasma"
"asmara" rotated
to the left by 5 becomes "aasmar"
"asmara" rotated
to the left by 6 becomes "asmara"
"asmara" rotated
to the left by 7 becomes "smaraa"
"asmara" rotated
to the left by 8 becomes "maraas"
similarly
"asmara" rotated
to the right by 0 becomes "asmara"
"asmara" rotated
to the right by 1 becomes "aasmar"
"asmara" rotated
to the right by 2 becomes "raasma"
"asmara" rotated
to the right by 3 becomes "araasm"
"asmara" rotated
to the right by 4 becomes "maraas"
"asmara" rotated
to the right by 5 becomes "smaraa"
"asmara" rotated
to the right by 6 becomes "asmara"
"asmara" rotated
to the right by 7 becomes "aasmar"
"asmara" rotated
to the right by 8 becomes "raasma"
and
etc…
*/
}
int main()
{
/*
This main program is designed to test the functions
you need to implement.
You should NOT remove any line of code from this main
program.
But you may add more test code in the main program if
you like.
*/
cout << "This program is designed to help you
test your functions." << endl;
srand(time(0));
//Test cstrlen function
cout << endl << "Testing cstrlen
function";
cout << endl << "------------------------"
<< endl;
char s1[] = "irregular";
cout << "The length of s1=\"" << s1
<< "\" is " << cstrlen(s1) << endl;
char emptyCstr[] = "";
cout << "The length of \"\" is " <<
cstrlen(emptyCstr) << endl;
//Test countChars functions
cout << endl << "Testing countChars
function";
cout << endl <<
"---------------------------" << endl;
char ch = 'r';
int count = countChars(s1, ch);
cout << "ch='" << ch << "' is found
in s1=\"" << s1 << "\" " << count << "
times." << endl;
/*//Test findChar functions
cout << endl << "Testing findChar
function";
cout << endl <<
"-------------------------" << endl;
int a = 2, b = cstrlen(s1);
int index = findChar(s1, ch, a, b);
cout << "ch='" << ch << "' is found
in s1=\"" << s1 << "\" in the index interval ["
<< a << ", " << b << ") at index " <<
index << endl;
a = 3;
index = findChar(s1, ch, a);
cout << "ch='" << ch << "' is found
in s1=\"" << s1 << "\" in the index interval ["
<< a << ", " << b << ") at index " <<
index << endl;
b = 8;
index = findChar(s1, ch, a, b);
cout << "ch='" << ch << "' is found
in s1=\"" << s1 << "\" in the index interval ["
<< a << ", " << b << ") at index " <<
index << endl;
index = findChar(s1, ch);
cout << "ch='" << ch << "' is found
in s1=\"" << s1 << "\" in the index interval [0, "
<< cstrlen(s1) << ") at index " << index <<
endl;
//Test getCopy function
cout << endl << "Testing getCopy
function";
cout << endl << "------------------------"
<< endl;
char* s2 = getCopy(s1);
cout << "A copy of \"irregular\" is s2=\""
<< s2 << "\"" << endl;
char* s3 = getCopy(s2);
cout << "A copy of s2=\"" << s2 <<
"\" is s3=\"" << s3 << "\"" << endl;
delete[] s2;
s2 = new char('\0');
cout << "s2 is modified to s2=\"" << s2
<< "\" but s3 is still s3=\"" << s3 << "\""
<< endl;
delete[] s3;
s3 = getCopy(s2);
cout << "A copy of s2=\"" << s2 <<
"\" is s3=\"" << s3 << "\"" << endl;
//Test rotateString function
cout << endl << "Testing rotateString
function";
cout << endl <<
"-----------------------------" << endl;
char s4[] = "asmara";
for (int i = 0; i < 10; i++)
{
int r = rand() % 101 - 50;
if (r > 0)
cout <<
"s4=\"" << s4 << "\" rotated " << r << "
times to the left becomes ";
else
cout <<
"s4=\"" << s4 << "\" rotated " << -r << "
times to the right becomes ";
rotateString(s4, r);
cout << "\"" << s4
<< "\"" << endl;
}
return 0;
}
Code:
//findchar
int findChar(const charPointer& s, const char& ch, const int& startIndex = 0, const int& lastIndexTemp = -1)
{
/*
returns the smallest index where the character ch is found in s starting from
startIndex (inclusive) upto lastIndex (exclusive). The default argument value
for startIndex is 0. The default argument value for lastIndexTemp is -1 in which
case cstrlen(s) must be used instead of -1.
For example,
findChar("test", 't', 1, 4) must return 3. Here startIndex = 1, lastIndex = 4
findChar("test", 't', 3) must return 3. Here startIndex = 3, lastIndex = 4
findChar("test", 't', 1, 3) must return -1. Here startIndex = 1, lastIndex = 3
findChar("test", 't') must return 0. Here startIndex = 0, lastIndex = 4
If ch is not found in s in the given interval, the the function must return -1
This function must first validate both the startIndex and lastIndex.
That is, if lastIndex > cstrlen(s) or startIndex < 0 it must return -1
*/
int lastIndex = lastIndexTemp;
if(lastIndexTemp==-1){
lastIndex=cstrlen(s);
}
if(lastIndex > cstrlen(s) || startIndex < 0){
return(-1);
}
for (int i=startIndex;i<lastIndex;i++){
if(s[i]==ch){
return(i);
}
}
return(-1);
}
//getCopy funtion
charPointer getCopy(const charPointer& s)
{
/*
returns a new cstring that is a copy of the cstring s.
That is a new cstring with as big memory as the size of
the cstring s is created and then all the characters of
s including the null char are copied to it.
*/
int len = cstrlen(s);
char *t = new char[len+1];
for(int i=0;i<len;i++){
t[i]=s[i];
}
t[len]='\0' ;
return(t);
}
//rotateString funtion
void rotateString(const charPointer& s, const int& r)
{
/*
Rotates the characters of s by r units
If r > 0, rotate the characters of s to the left
If r < 0, rotate the characters of s to the right
Please note the value of r can be any integer even larger than the length of s
For example,
"asmara" rotated to the left by 0 becomes "asmara"
"asmara" rotated to the left by 1 becomes "smaraa"
"asmara" rotated to the left by 2 becomes "maraas"
"asmara" rotated to the left by 3 becomes "araasm"
"asmara" rotated to the left by 4 becomes "raasma"
"asmara" rotated to the left by 5 becomes "aasmar"
"asmara" rotated to the left by 6 becomes "asmara"
"asmara" rotated to the left by 7 becomes "smaraa"
"asmara" rotated to the left by 8 becomes "maraas"
similarly
"asmara" rotated to the right by 0 becomes "asmara"
"asmara" rotated to the right by 1 becomes "aasmar"
"asmara" rotated to the right by 2 becomes "raasma"
"asmara" rotated to the right by 3 becomes "araasm"
"asmara" rotated to the right by 4 becomes "maraas"
"asmara" rotated to the right by 5 becomes "smaraa"
"asmara" rotated to the right by 6 becomes "asmara"
"asmara" rotated to the right by 7 becomes "aasmar"
"asmara" rotated to the right by 8 becomes "raasma"
and etc…
*/
int ln = cstrlen(s);
int rc = r%ln; //getting original count of rotation
if(r<0){
rc = (-r);
rc=rc%ln;
rc=ln-rc;
}
char res[ln];
int ind;
for(int i = 0; i<ln ; i++ ){
ind = (rc+i)%ln;
res[i] = s[ind];
}
for(int i=0;i<ln;i++){
s[i]=res[i];
}
delete [] res;
}
Output:
Testing findChar function
-------------------------
ch='r' is found in s1="irregular" in the index interval [2, 9) at index 2
ch='r' is found in s1="irregular" in the index interval [3, 9) at index 8
ch='r' is found in s1="irregular" in the index interval [3, 8) at index -1
ch='r' is found in s1="irregular" in the index interval [0, 9) at index 1
Testing getCopy function
------------------------
A copy of "irregular" is s2="irregular"
A copy of s2="irregular" is s3="irregular"
s2 is modified to s2="" but s3 is still s3="irregular"
A copy of s2="" is s3=""
Testing rotateString function
-----------------------------
s4="asmara" rotated 40 times to the left becomes "raasma"
s4="raasma" rotated 40 times to the right becomes "asmara"
s4="asmara" rotated 21 times to the left becomes "araasm"
s4="araasm" rotated 37 times to the left becomes "raasma"
s4="raasma" rotated 30 times to the left becomes "raasma"
s4="raasma" rotated 25 times to the right becomes "araasm"
s4="araasm" rotated 47 times to the left becomes "maraas"
s4="maraas" rotated 40 times to the left becomes "asmara"
s4="asmara" rotated 2 times to the left becomes "maraas"
s4="maraas" rotated 30 times to the right becomes "maraas"
_______________________
Hope it Helps.