Question

In: Computer Science

You are required to use C++ static or dynamic arrays of characters to store c-strings. You...

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;

}

Solutions

Expert Solution

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); }


Related Solutions

You are required to use C++ static or dynamic arrays of characters to store c-strings. You...
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...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files 1. WriteaclassGradeBookcontainingthefollowing: Private attributes: - courseName: a string representing the name of the course. - nbOfStudents: an integer representing the number of students enrolled in the course. The number of students is greater than or equal to 5. - grades: a double dimensional array of integers representing the grades of Test1, Test2 and Final of every student. It should be a dynamic array. Public...
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);....
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);. Code a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered .The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Eg: Enter a sequence of characters: abAa1121X+&$%$dc[space] Distinct characters are: abA12X+&$%dc. Use C++
answer in JAVA language please In this assignment you will use: one-dimensional arrays; strings and various...
answer in JAVA language please In this assignment you will use: one-dimensional arrays; strings and various string-handling methods; file input and output; console input and output; loops; and conditional program statements. Instruction You are asked to write a program that translates a Morse code message into English language. The program will ask the user to enter filenames for two input files, one for the code translation table data and the other for the coded message data, as well as an...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the...
In C++ please modify the following program and add characters (char) and names (strings) to be...
In C++ please modify the following program and add characters (char) and names (strings) to be added to the linked list along with integers. The current demo program accepts only integer data, so you would ask the user to select the data type to be added to the linked list. The user should be given the following three choices: (a) whole numbers (b) single characters (c) strings Once the user makes a selection from the list above then your program...
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Why would you choose to use dynamic (shared) library linkage compared to static? Identify at least...
Why would you choose to use dynamic (shared) library linkage compared to static? Identify at least two advantages of dynamic over static. Describe at least one case where static is preferable. If it “feels” like all the advantages of each have already been listed by your classmates, comment on other students’ postings indicating why you support their perspective.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT