Description of this paper

This program will look for an individual character

Description

solution


Question

This program will look for an individual character, called the key character, inside a target string. It will then perform various functions such as replacing the key character with a dash (-) wherever it occurs in the target string. For example, if the key character were;a;and the string were;He who laughs last, laughs fast, faster, FASTEST.;then the operation of replacing the 'a' by a dash would result in the new string;He who l-ughs l-st, l-ughs f-st, f-ster, FASTEST.;As you can see, only the lower-case 'a' was detected and replaced. This is how we will do things in option A.;This was only one possible task we might perform. Another would be to remove all instances of the key character rather than replace each with a dash. Yet a third might be to count the number of key characters. We are going to do it all.;Modifying vs. Rebuilding;Whenever we deal with strings, we have to decide whether we are going to modify an existing string or create a second string which has the desired changes. We will not attempt to touch the original string in our operations. Instead, we will declare a new string object and build that up in stages, replacing or removing the desired characters of the original string by simply taking action on the new string we are building. When I say we "build" a string, I mean that we initialize the string to be empty, "", and then use concatenation to replace it with ever-longer versions of itself. This technique has some advantages that allow it to be used for a variety of purposes, so it's good to learn now.;For example, consider this statement, which appends an exclamation point to the end of a string, myStr;myStr = myStr + "!;This statement uses the old value of myStr on the RHS, then completely throws away the old value on the LHS and replaces it with the new, longer, string. This is similar to a more familiar kind of numeric statement;n = n + 3;where we replace the old contents of n with new contents.;The Methods;We will be writing methods. Some will get input from the user (which take no parameters) and others will take arguments: the string and/or key character. Depending on the method we write, it will return either another string or an integer. For example, one of the methods we write will take the key character and the target string as parameters and will return a new string which has all the occurrences of the key character replaced by dashes. Its signature would look like this;string maskLetter(string theString, char keyLetter)We will be careful at all stages: input methods will only deal with user input, and not attempt to do computation. Computations will not do any input or output.;The exception is always main(). In main() we may do input and output directly if we are not required to use a method to do so by the spec. In our spec, this week, we will use input methods to get the input (not main()), but we will allow main() to do the output directly.;The Program Spec;Ask the user to enter both a key character, and also a target string (phrase, sentence, etc.). Then, show the user three things;1.The target string with the key character replaced by dashes.;2.The target string with the key character removed.;3.The number of occurrences of the key character (case sensitive) in the target string.;This program does not loop for different strings. Once it processes a string, it ends.;Input Method Specs;char getKeyLetter();This method requests a single character from the user and continues to ask for it until the user gets it right: this method will test to make sure the user only types one single character. 0, 2, 3 or more characters will be flagged as an error and the method will keep at the user until he types just one character. You are not required to use a char as an input variable -- in fact, you cannot solve the problem using a char as input (you must think about this and make the appropriate choice here). What matters is that a char is returned, as a functional return, to the client, main().;string getString();This method requests a string from the user and continues to ask for it until the user gets it right: this method will test to make sure the user only types a string that has at least 4 letters. The acquired string will be returned as a functional return.;Processing Method Specs;string maskLetter(string theString, char keyLetter);This method will take both a string and a character as parameters and return a new string that has each occurrence of the key character replaced by a dash, '-'.;string removeLetter(string theString, char keyLetter);This method will take both a string and a character as parameters and return a new string that has each occurrence of the key character removed, but all other characters left intact;/* ---------------------- Sample run ---------------------------------------;Please enter a SINGLE letter to act as key: abc;Please enter a SINGLE letter to act as key;Please enter a SINGLE letter to act as key: a;Please enter a phrase or sentence >= 4 letters;He who laughs last, laughs fast, faster, FASTEST.;String with 'a' masked;He who l-ughs l-st, l-ughs f-st, f-ster, FASTEST.;# as: 5;String with 'a' removed;He who lughs lst, lughs fst, fster, FASTEST.

 

Paper#68242 | Written in 18-Jul-2015

Price : $32
SiteLock