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;
}
Function third
For the third function you shoud traverse the character array and while traversing the array keep checking if the current visited index contains the desired element or not if you finds the element simply return the value of index; to maintain the index mainain a variable; if you do get that element and entire array is traversed simply return the default value as mentioned in the comment. not to get the length you can use the cstrlen() function.
Function four
in this case you should call the length function in order to get the desired length of character array, or simply traverse the given array to get the count of elements, so that you can have initialize copy array, or simply hard code the length according to constraints with upper limit, then traverse the given array and copy array and inserting the value of given array's selected index to selected index in copy array till you reach the end of character array at last simply return the address of copy array, note do not forget to insert the null character at the end.
Fifth Function
in rotate function make a helper function that rotates the array ones , in this maintain a temp variable and store value of first index in temp variable, now simply store the value of i+1 index to ith index, use a loop for this at last at store the temp data to last index; in parent function call this function r times for that use loop.
leftRotatebyOne(int arr[], int n) { int temp = arr[0], i; for (i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; arr[i] = temp; } *Function to left rotate arr[] of size n by d*/ void leftRotate(int arr[], int d, int n) { for (int i = 0; i < d; i++) leftRotatebyOne(arr, n); }