Provide full C++ Code: 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. Place all of the functions in a single file and then (in the same file) create a main() function that tests the functions thoroughly. Please note the following: You may not use any variables of type string such as #include <string>. You may not use any c-string functions other than strlen(). 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. 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 for testing purposes will probably be main(). The only requirement about main() is that it sufficiently test your functions. You can get user input in main() to use as arguments in the function calls, or use hard-coded values. 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. For example: char str1[] = "Hello World"; char str2[] = "C++ is fun!"; Don't try to create and initialize a c-string on one line using pointer notation, like this: char* str1 = "Hello world"; 5. Since it is just being used for testing, it's fine to have a very long main() function. You can do anything you want to test the functions, as long as the end result is that you demonstrate that they work correctly. 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. Do not get confused by white space characters, punctuation, or digits, they should not get any special treatment. 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. Even if the for loop is empty, that's 10,000 operations when it could easily have been done in 100 operations. 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 unnecessarily traverses the array. 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++; }
Provide full C++ Code:
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. Place all of the functions in a single file and then (in the same file) create a main() function that tests the functions thoroughly.
Please note the following:
-
-
You may not use any variables of type string such as #include <string>. You may not use any c-string functions other than strlen().
-
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.
-
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 for testing purposes will probably be main(). The only requirement about main() is that it sufficiently test your functions. You can get user input in main() to use as arguments in the function calls, or use hard-coded values.
-
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. For example:
char str1[] = "Hello World";
-
char str2[] = "C++ is fun!";
Don't try to create and initialize a c-string on one line using pointer notation, like this:
char* str1 = "Hello world";
5. Since it is just being used for testing, it's fine to have a very long main() function.
You can do anything you want to test the functions, as long as the end result is that you demonstrate that they work correctly.
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.
Do not get confused by white space characters, punctuation, or digits, they should not get any special treatment.
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. Even if the for loop is empty, that's 10,000 operations when it could easily have been done in 100 operations.
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 unnecessarily traverses the array. 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++;
}
Unlock instant AI solutions
Tap the button
to generate a solution