Question: /*Please display the code and output. */ Phase 1 Implement the following functions. Each function deals with null terminated C-Style strings. You can assume that
/*Please display the code and output. */
Phase 1
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 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 may not use any variables of type string. This means that you should not #include
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.
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.
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.
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 the index in string s where the substring can first be found. For example if s is "Skyscraper" and substring is "ysc" the function would return 2. It should return -1 if the substring does not appear in the string. This is the hardest function in phase 1. My solution is under 10 lines, but if you find yourself going up to around 15 or even 20 I wouldn't penalize you.
int findSubstring(const char* inString, const char substring[])
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. 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)
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)
Phase 2
Now implement the following two functions in the same file as your phase 1 functions, and add statements to main() to test them. As with the phase 1 functions, these will have no output, and only read() will have input. The purpose of this part of the assignment is to give you practice using strlen(), strcpy(), and strcat(). For full credit you must use these functions when appropriate. This means, among other things, that there will be no loops. Also, don't use related functions such as strncpy() or strcpy_s(). You still may not use anything from the C++ string class.
Unfortunately, Visual C++ will, under its default settings, report an error when you try to use strcpy() or strcat(), even though they are standard C++. I don't have access to VS, but I believe that if you put the following as the first line in your file, that won't happen:
#pragma warning(disable:4996)
Someone please let me know if that doesn't work.
1)
void read(char*& readMe);
This function should use cin.getline() to read a c-string from the console into the parameter, readMe. The function will stop reading when a newline character is encountered, and cin.getline() does this. You can read about cin.getline() near the end of section 10.3 in the text (page 551 in 7th edition. Note: this is the text by Gaddis. If your class is using a different text you'll need to use the index). This read() function has a precondition that readMe is either NULL or has been allocated using the "new" operator. (This is so that you can call "delete" on it without worrying about whether it is uninitialized.) You are required to allocate the exact correct number of elements for readMe that are required to store the string that the user enters, but you may assume that the user enters no more than 80 characters.
Don't let the char*& notation intimidate you. Think of it this way. If we leave off the &, then what we have is a parameter of type "pointer to char" that is passed by value. When we add the &, it just means that we have a parameter of type "pointer to char" that is passed by reference. We want pass by reference because when we change readMe inside this function, we want the corresponding argument in the calling function to also change. One of the statements in this function will start with "readMe = ", and if readMe was not passed by reference this wouldn't have the desired effect of changing the corresponding argument in the calling function.
This function will be 5 lines of code:
Deallocate readMe, because we are going to have to allocate a new chunk of memory for it. (Make sure you understand this point!!)
Declare a non-dynamic array of size 81.
Use getline() to read the user's input into this non-dynamic array.
Allocate readMe to be the correct size. You'll need "new", and strlen().
Use strcpy() to copy the contents of the non-dynamic array into readMe.
You'll want to test the function more thoroughly than just this, but to start with try something like this:
char* mystr = NULL; cout << "Enter a string: "; read(mystr); cout << "You entered: " << mystr << endl;
2)
void concatenate(char*& left, const char* right);
This function modifies left so that it is equal to the concatenation of left and right. As with read(), concatenate() has a precondition that "left" is either NULL or has been allocated using the "new" operator (this is so that it is safe to use "delete" on it). Be careful to allocate left to be the correct size first. Also make sure there are no memory leaks.
Note that when you call this function from main(), the first argument cannot be a c-string that you have declared as I suggested above:
char str1[] = "Hello World";
Instead, use something like
char* str1 = new char[12]; strcpy(str1, "Hello World");
The reason (definitely a good thing if you can follow this, but it shouldn't affect your ability to complete this assignment if you don't): Remember that an array name is a pointer constant. That means if you declare something as an array, you can treat it like a pointer in every way EXCEPT you can't change what it is actually pointing to. In the case of concatenate, in order to do it correctly we need to actually change what the parameter is pointing at (because we need to allocate new memory for it).
/*Please display the code and output. */
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
