Related questions
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:
-
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) -
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) -
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) -
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) -
This function converts the c-string parameter to all uppercase.
void toupper(char* inString) -
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++; }Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images
- Jupyter Notebook Fixed Income - Certicificate of Deposit (CD) - Compound Interest Schedule An interest-at-maturity CD earns interest at a compounding frequency, and pays principal plus all earned interest at maturity. Write a function, called CompoundInterestSchedule, that creates and returns a pandas DataFrame, where each row has: time (in years, an integer starting at 1), starting balance, interest earned, and ending balance, for an investment earning compoundedinterest. Use a for(or while) loop to create this table. The equation for theith year's ending balance is given by: Ei =Bi (1+r/f)f where: Ei is year i's ending balance Bi is year i's beginning balance (note: B1 is the amount of the initial investment (principal) r is the annual rate of interest (in decimal, e.g., 5% is .05) f is the number of times the interest rate compounds (times per year) The interest earned for a given year is Ei - Bi Note the term of the investment (in years) is not in the above equation; it is used...arrow_forwardvowelIndex Write a function vowelIndex that accepts a word (a str) as an argument and returns the index of the first vowel in the word. If there is no vowel, -1 is returned. For this problem, both upper and lower case vowels count, and 'y' is not considered to be a vowel. Sample usage:>>> vowelIndex('hello') 1>>> vowelIndex('string') 3>>> vowelIndex('string') 3>>> vowelIndex('BUY') 1>>> vowelIndex('APPLE') 0>>> vowelIndex('nymphly') -1>>> vowelIndex('string')==3 True>>> vowel Index ('nymphly')==-1 Truearrow_forwardt. Given a string as a parameter to the function, return a square grid filled with all the characters of the string from left to right. Fill empty spaces in the grid with '_'. Sample Input: stringtogrid Output: {{s, t, r, i}, {n, g, t, o}, {g, r, i, d}, {_, _, _, _}} Complete the function: vector<vector<int> formGrid(string s){}arrow_forward
- JS Write a function named find_value that takes a list/array of whole numbers as a parameter. Your functio will need to find if the parameter contains the value 3 as an entry. Return true if the input has an entry equivalent to 3 and false if it does not.arrow_forwardJS Write a function named list_concat that has a single parameter. This parameter will be a list/array. The parameter's entries will be strings. Your function needs to return a string. The returned string's value should be the concatention of all of the entries in the parameter with a space after each entry. Sample test cases: list_concat(["single"]) evaluates to "single list_concat(["limit", "break", "ready"]) evaluates to "limit break readyarrow_forwardUse C++ Using dynamic arrays, implement a polynomial class with polynomial addition, subtraction, and multiplication.Implement addition and subtraction functions first. it should have five filesOne is the main.cpp fileOne is the Polynomial.cpp file it'sit's in the pictureOne is the Polynomial.h file One is A.txtOne is B.txt The contents of A and B.txt are in the picture Here is the main.cpp code I have: #include <iostream>#include <fstream>#include "polynomial.h"using namespace std;int main(){// read data from A.txtifstream infile;infile.open("A.txt");if (infile.fail()) {cout << "Input file opening failed." << endl;exit(1);}int degree_A;infile >> degree_A;int* coef_A = new int[degree_A+1];for (int i = 0; i < degree_A + 1; i++)coef_A[i] = 0;int coef, exp;while (!infile.eof()){infile >> coef >> exp;coef_A[exp] = coef;}infile.close();// read data from B.txtinfile.open("B.txt");if (infile.fail()) {cout << "Input file opening failed."...arrow_forward
- This assignment will give you practice on basic C programming. You will implement a few Cprogramsarrow_forwardJS Write a function named sum_between_indices whose parameter is a list/array of decimal numbers. Your function must return the sum of the entries at index 4 through index 15. Make certain to include the entries at index 4 and at index 15 in your sum. You SHOULD assume the parameter will have more than 15 entries.arrow_forwardWrite a statement that declares a dynamic array of 5 strings and lets a pointer variable p to point to that array A/arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education