Question

In: Computer Science

NOTE: PLEASE READ THE FIRST 4 GUIDELINES ON DOING THIS ASSIGNMENT IT'S A C++ CLASS, THE...

NOTE: PLEASE READ THE FIRST 4 GUIDELINES ON DOING THIS ASSIGNMENT

IT'S A C++ CLASS, THE FUNCTIONS ARE PROVIDED BELOW

Assignment 7.1 [45 points]

No initial file comment is required for this assignment. Function comments are still required.

Implement the following functions. Each function deals with null terminated C-Style strings. You can assume that any char array passed into the functions will contain null terminated data. Place all of the functions in a single file and then (in the same file) create a main() function that tests the functions thoroughly. You will lose points if you don't show enough examples to convince me that your function works in all cases.

Please note the following:

You can do anything you want to test the functions, as long as the end result is that you demonstrate that they work correctly.

  1. You may not use any variables of type string. This means that you should not #include <string>. Also, you may not use any c-string functions other than strlen(). If you use any other c-string functions, you will not get credit. Note, however, that functions such as toupper(), tolower(), isalpha(), isspace(), and swap() are NOT c-string functions, so you can use them. Also note that this prohibition is only for the functions that you are assigned to write. You can use whatever you want in your main() function that tests the assigned functions.

  2. In most cases it will be better to use a while loop that keeps going until it hits a '\0', rather than using a for loop that uses strlen() as the limit, because calling strlen() requires a traversal of the entire array. You could lose a point or two if you traverse the array unnecessarily.

  3. None of these function specifications say anything at all about input or output. None of these functions should have any input or output statements in them. The output should be done in the calling function, which will probably be main(). The only requirement about main() is that it sufficiently test your functions. So, you can get user input in main() to use as arguments in the function calls, or you can use hard-coded values -- up to you, as long as the functions are tested thoroughly.

  4. Here's a hint about how to work with c-strings in main(). There are several different ways that you could assign values to c-string variables, but I think the easiest is just hardcoding a lot of examples. For example:

    char str1[] = "Hello World";
    char str2[] = "C++ is fun!";
    

    Whatever you do, don't try to create and initialize a c-string on one line using pointer notation, like this:

    char* str1 = "Hello world";
    

    This is dangerous (and officially deprecated in the C++ standard) because you haven't allocated memory for str1 to point at.

  5. Since it is just being used for testing, In this case it is fine to have a very long main() function.

Here are the functions:

  1. This function finds the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. The function should be case sensitive (so 'b' is not a match for 'B').

    int lastIndexOf(const char* inString, char target)
    
  2. This function alters any string that is passed in. It should reverse the string. If "flower" gets passed in it should be reversed in place to "rewolf". For efficiency, this must be done "in place", i.e., without creating a second array.

    void reverse(char* inString)
    
  3. This function finds all instances of the char 'target' in the string and replace them with 'replacementChar'. It returns the number of replacements that it makes. If the target char does not appear in the string it should return 0.

    int replace(char* inString, char target, char replacementChar)
    
  4. This function returns true if the argument string is a palindrome. It returns false if it is no. A palindrome is a string that is spelled the same as its reverse. For example "abba" is a palindrome. So are "hannah" and "abc cba".

    Do not get confused by white space characters, punctuation, or digits. They should not get any special treatment. "abc ba" is not a palindrome. It is not identical to its reverse.

    Your function should not be case sensitive. For example, "aBbA" is a palindrome.

    You must solve this problem "in place", i.e., without creating a second array. As a result, calling your reverse() function from this function isn't going to help.

    bool isPalindrome(const char* inString)
    
  5. This function converts the c-string parameter to all uppercase.

    void toupper(char* inString)
    
  6. This function returns the number of letters in the c-string.

    int numLetters(const char* inString)

Solutions

Expert Solution

//C++ program

#include<iostream>
#include<string.h>
using namespace std;

int lastIndexOf(const char* inString, char target){
   for(int i=strlen(inString)-1;i>=0;i--){
       if(inString[i] == target)return i;
   }
   return -1;
}
void reverse(char* inString){
   int i=0 , j = strlen(inString)-1;
   while(i<j){
       char temp = inString[i];
       inString[i] = inString[j];
       inString[j] = temp;  
       i++;
       j--;
   }
  
}
int replace(char* inString, char target, char replacementChar){
   int i;
   for(i=0;i<strlen(inString);i++){
       if(inString[i] == target)break;
   }
   if(i==strlen(inString))return 0;
   inString[i] == replacementChar;
   return i;
}
bool isPalindrome(const char* inString){
   int i=0 , j = strlen(inString)-1;
   while(i<j){
       if(isalpha(inString[i]) && isalpha(inString[j])){
           if(toupper(inString[i])!= toupper(inString[j]))return false;
       }
       else if(inString[i] != inString[j])return false;
       i++;
       j--;
   }
   return true;
}

void toupper(char* inString){
   for(int i=0;inString[i]!='\0';i++){
       char ch = inString[i];
       if(ch>='a' && ch<='z')inString[i] = (ch-'a')+'A';
   }
}

int numLetters(const char* inString){
   int count=0;
   for(int i=0;inString[i]!='\0';i++){
       if (isalpha(inString[i]))count++;
   }
   return 0;
}
int main(){
   char str1[] = "Hello World";
   char str2[] = "aBbA";
  
   if(isPalindrome(str2))cout<<"Yes Palindrome";
   else cout<<"No\n";
}


Related Solutions

Please note that this assignment is a “Public Health class under social structure and health”. Therefore...
Please note that this assignment is a “Public Health class under social structure and health”. Therefore answer must be related to the class subject. Respond to the following questions (250 words minimum). Thinking about where you live: Are social determinants of health visible? What are they? What are some changes that could happen in your neighborhood that would help improve health outcomes for you and your neighbors?  
Please note that this assignment consists of two separate parts. The first part gives the cash...
Please note that this assignment consists of two separate parts. The first part gives the cash flows for two mutually exclusive projects and is not related to the second part. The second part is a capital budgeting scenario. Part 1 Please calculate the payback period, IRR, MIRR, NPV, and PI for the following two mutually exclusive projects. The required rate of return is 15% and the target payback is 4 years. Explain which project is preferable under each of the...
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
Write a Fraction Class in C++ PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE,...
Write a Fraction Class in C++ PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE, DON'T ANSWER IT IF YOU'RE NOT SURE WHAT YOU'RE DOING. I APPRECIATE IF YOU WRITE COMMENTS AS WELL. WRONG ANSWER WILL GET A DOWNVOTE Thank in Advance. Must do; The class must have 3 types of constructors; default, overloaded with initializer list, copy constructor You must overload the assignment operator You must declare the overloaded output operator as a friend rather than part of...
In C++ please Your class could have the following member functions and member variables. However, it's...
In C++ please Your class could have the following member functions and member variables. However, it's up to you to design the class however you like. The following is a suggestion, you can add more member variables and functions or remove any as you like, except shuffle() and printDeck(): (Note: operators must be implemented) bool empty(); //returns true if deck has no cards int cardIndex; //marks the index of the next card in the deck Card deck[52];// this is your...
Note: Not sure what subject this should be under. It's from a business law class. The...
Note: Not sure what subject this should be under. It's from a business law class. The Happy Times Bar and Restaurant was located on a busy downtown street. The front part of the premises consisted of the bar and a few small tables where patrons were served drinks. The rear part of the building housed the restaurant, and a patron who wished to obtain a meal at the restaurant was required to pass through the bar room to reach the...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius
PLEASE READ CAREFULLY BEFORE STARTING THE ASSIGNMENT!!! I ALREADY HAVE ANSWERS FOR THE FIRST JOURNAL TABLE...
PLEASE READ CAREFULLY BEFORE STARTING THE ASSIGNMENT!!! I ALREADY HAVE ANSWERS FOR THE FIRST JOURNAL TABLE AS YOU CAN SEE BELOW, BUT I DON'T HAVE THE ANSWER FOR THE SECOND JOURNAL TABLE. I FOUND MANY ANSWERS FOR THE SECOND TABLE HERE ON CHEGG. BUT NOT ONE HAS A CORRECT ANSWER. FOR SOME OF THEM IT'S COMPLETELY DIFFERENT ANSWERS ALTHOUGH THE QUESTION IS THE SAME. PLEASE ANSWER THAT TABLE CAREFULLY AND CORRECTLY. USE THE SAME EXACT TABLE THAT I PROVIDED BELOW...
Accounting II AC2034 ASSIGNMENT 2 PAYROLL Please Note: This assignment is an INDIVIDUAL assignment The payroll...
Accounting II AC2034 ASSIGNMENT 2 PAYROLL Please Note: This assignment is an INDIVIDUAL assignment The payroll records of Acme Company provided the following information for the weekly pay period ended March 20, 2020: Employee     Employee     Hours Worked         Rate of Pay Hospital        Union             Earnings to End    Name                No            For Week                                          Insurance     Dues             of Previous Week Bugs Bunny       40                    40                            $34                   $30.00        $12.00           $43,000 Sylvester            50                    42                              36            ...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream&...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream& operator<<(ostream&, const Prescription&);    friend istream& operator>>(istream&, Prescription&);    private: int idNum; int numPills; double price;    public: Prescription(const int = 0, const int = 0, const double = 0.0); }; Prescription::Prescription(const int id, const int pills, const double p) {    id = id;    numPills = pills;    price = p; } ostream& operator<<(ostream& out, const Prescription pre) {    out <<...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT