2. Consider the problem of replacing all straight quotes \"like this\" with curl
ID: 3752980 • Letter: 2
Question
2. Consider the problem of replacing all straight quotes "like this" with curly quotes “like this” in a string. Before embarking on this task, let’s think of a reusable method. Suppose we know where the straight quote is. Then we want to replace it. That would be useful in other situations too.
Your task is to complete the function replace_at that replaces the character at a given position with an arbitrary string.
3. Consider again the problem of replacing all straight quotes "like this" with curly quotes “like this” in a string. We need to know the position of the first quotation mark, then the second quotation mark. We could write functions for these tasks, but we can do better. In general, we may want to look for a character and find the nth occurrence.
Your task is to complete the function find_occurrence that returns the nth occurrence of a character within a string.
4. Now we are ready to tackle the problem of replacing all pairs of straight quotes "like this" with curly quotes “like this” in a string. Using the helper functions, replace_at and find_occurrence, from the two preceding exercises, implement the replace_quotesfunction below.
For example, to find the first quotation mark, call
To replace it with an opening curly quote, call
If there is an odd number of straight quotes, don't replace the last one.
Note that the left and right curly quotes each use two bytes of memory, so the length of the string changes when you replace a straight quote. This will be explained more fully in Chapter 8.
5.
“Wiki markup” is a simple convention indicating which parts of text should be bold, italic, computer code, and so on. The symbols used for each feature vary. We will use these simple rules:
Bold is indicated as *Bold*
Italic is indicated as !Italic!
Code is indicated as `Code`
Superscript is indicated with ^, such as 10^23^ for 1023
Subscript is indicated with _, such as H_2_O for H2O
To include one of these symbols in the text, precede it by a backslash, such as !Hello!.
In order to see the text, we would like to translate the symbols into HTML tags. For example, the `main` function should turn into the <code>main</code> function.
Rearrange the following lines of pseudocode that decomposes this problem into simpler tasks (finding, replacing, and removing characters).
Assume that find position of ... at or after ... returns -1 if there is no match.
Replace the symbol at the end first. (If you replace the start tag, the end position shifts.)
Press start to begin.
Start
while not done
else
start = find position of *!`^_ at or after start
symbol = character at position start
if start == -1
end = find position of symbol at or after start + 1
tag = tag for symbol
done = true
done = false
Replace character at start with "<" + tag + ">"
if end -1
Replace character at end with "</" + tag + ">"
Remove all backslashes
start = 0
In the preceding exercise, you started the process of stepwise refinement for the task of translating Wiki markup to HTML. Let us continue the process.
One of the steps was to find position of next *!`^_ at or after start. We want to turn this into a function call next_symbol(message, symbols, start) where symbols is a string of the symbols that we are looking for (such as "*!`^_"). We want to return the position of the first of these symbols in the message at position start.
Rearrange the following lines to produce pseudocode for this function.
Press start to begin.
Start
return -1
while pos < length of message
else if symbols contains ch
return pos
pos = pos + 2
if ch is a backslash
pos++
else
ch = message.substr(pos, 1)
pos = start
In the preceding exercises, you saw how one can use stepwise refinement to solve the problem of translating Wiki markup to HTML. Now turn the pseudocode into code. Complete the convert_to_HTML and next_symbol functions.
Explanation / Answer
Answer 2:
import java.util.*;
import java.lang.*;
class Demo
{
public static void main (String[] args) throws java.lang.Exception
{
String str = "Param is the string.";
System.out.println(replaceAt(str, 5, "PARAM "));
}
public static String replaceAt(String str, int pos, String re)
{
if (str.equals(null) || str.equals("") && pos < str.length())
{
return str;
}
else
{
return str.substring(0, pos)+ ", " + re +str.substring(pos + 1);
}
}
}
Output: Param, PARAM is the string.
Answer 3:
import java.util.*;
import java.lang.*;
class Demo
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(find_occurrence("param",'p',1));
}
public static int find_occurrence(String str, char ch, int n)
{
int count = 0;
for (int i = 0; i < str.length(); i++)
{
if (str.substr(i, 1) == ch)
{
count++;
if (count == n) { return i; }
}
}
return -1;
}
}
Output: 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.