Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

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.

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 not. 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" and "b" and "n$aa$n" and "" (empty string).

    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. (Note, this may be different from other definitions of palindrome. The reason for choosing this definition is actually to make the function easier: you can just write the code without ever thinking about whether the characters are letters or not. If you had to skip over spaces and other non-letters, the code would be more complex.)

    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)

The statement

for(int i = 0; i < strlen(string); i++)

is super, super inefficient. Let's say the string is 100 characters long. This for loop, then, will iterate 100 times. but each time it iterates, it calls strlen() again. Calling strlen() requires another 100 operations (it goes through the array until it finds the null character). So, even if the for loop is empty, that's 100 X 100 = 10,000 operations, when it could easily have been done in 100 operations.

However, I'm talking about a more subtle problem. Let's say you are writing a function that goes through and changes each character in the array to a dollar sign ($). You can avoid the super, super inefficient loop by calling strlen() ahead of time, like this:

int stringLength = strlen(string); for(int i = 0; i < stringLength; i++){ string[i] = '$'; }

However, the above solution still traverses the array unnecessarily. It traverses the array once to find the length of the string, and then it traverses the array again to actually perform the task. A much better solution is to avoid calling strlen() at all (and thus avoid traversing the array unnecessarily):

int i = 0; while (string[i] != '0円') { string[i] = '$'; i++; }
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education