Question

3. (24%) Write the following functions. You MUST use recursion for credit. You may use the string library functions: indexing, lengthO, +, and substr(int n) (recall that substr(n) returns a string with all but the first n characters). (a) string alternate(string s): returns a string that contains every other element of s starting at the beginning. For example, alternate( dog ) dg (b) string uc(string s): returns a string that is the same as s except that every upper case character is replaced with a UC Hint: assume you have the uc of the strings tail. (c) string uc2(string s, string repl): same as above, but replace it with repl instead of UC

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi Student,

Please find below the solutions.

a.

private static String alternate(String str) {

String res = "";

for(int i=0; i<str.length();i++){

res+=str.charAt(i);

i++;

}

return res;

}

b.

private static String uc(String str1) {

String res = "";

for(char c : str1.toCharArray()) {

if(Character.isUpperCase(c)) {

res+="UC";

}

else{

res+=c;

}

}

return res;

}

c.

private static String uc2(String str1) {

String res = "";

for(char c : str1.toCharArray()) {

if(Character.isUpperCase(c)) {

res+="repl";

}

else{

res+=c;

}

}

return res;

}

Here is a complete program that you can run to see how exactly it is working.

import java.util.Scanner;

public class TriangleNumber {

public static void main(String[] args) {

String str = "dog";

System.out.println(alternate(str));

String str1 = "UpperCase";

System.out.println(uc(str1));

  

String str2 = "UpperCase";

System.out.println(uc2(str2));

}

private static String uc(String str1) {

String res = "";

for(char c : str1.toCharArray()) {

if(Character.isUpperCase(c)) {

res+="UC";

}

else{

res+=c;

}

}

return res;

}

private static String uc2(String str1) {

String res = "";

for(char c : str1.toCharArray()) {

if(Character.isUpperCase(c)) {

res+="repl";

}

else{

res+=c;

}

}

return res;

}

private static String alternate(String str) {

String res = "";

for(int i=0; i<str.length();i++){

res+=str.charAt(i);

i++;

}

return res;

}

}

Happy Learning . If you like this answer, give a thumbs up!

Add a comment
Know the answer?
Add Answer to:
3. (24%) Write the following functions. You MUST use recursion for credit. You may use the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc....

    you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc. You must write your own. You might consider writing helper functions to do tasks that many of these functions require, e.g. finding the last character of the string, and then use those helper functions where convenient. This function finds all instances of the char ‘target’ in the string and replaces them with ‘replacementChar’. It also returns the number of replacements that it makes. If...

  • Implement the following Racket functions: (You must use recursion, and not iteration. You may not use...

    Implement the following Racket functions: (You must use recursion, and not iteration. You may not use side-effects (e.g. set!). ) 1. Reflexive? Input: a list of pairs, L and a list S. Interpreting Las a binary relation over the set S, Reflexive? returns #t if L is a reflexive relation over the set S and #f otherwise. Examples: (display "Reflexive?\n") (Reflexive? '((a a) ( bb) (cc)) '(a b c)) ---> #t (Reflexive? '((a a) (bb)) '(a b c)) ---> #f...

  • C++ Using Recursion We are going to create a (looping) menu that accesses functions that use...

    C++ Using Recursion We are going to create a (looping) menu that accesses functions that use recursion. The function headers and a description will be provided. You are responsible for defining the functions. Ensure that they each contain a base case they reach that doesn’t have a recursive function call, otherwise it’s an infinite loop! Functions must be implemented with recursion. int Factorial(int arg) Returns arg! (4! Is 4 * 3 * 2 = 24) Base case is Factorial(1) returning...

  • Write a class called StringCode with the following functions. (Do not change these function names or...

    Write a class called StringCode with the following functions. (Do not change these function names or return types, else tests will fail). public static String blowup (String str) Returns a version of the original string as follows: each digit 0-9 that appears in the original string is replaced by that many occurrences of the character to the right of the digit. So the string "a3tx2z" yields "attttxzzz" and "12x" yields "2xxx". A digit not followed by a character (i.e. at...

  • In C++: You are to write two functions, printString() and testString(), which are called from the...

    In C++: You are to write two functions, printString() and testString(), which are called from the main function. printString (string) prints every character in the string to std::cout with a space after every character and a newline at the end. testString (string) returns true if the string contains characters that are in sorted order, false otherwise. You may assume that all characters are lowercase and only alphabetical characters are present. See the main() to see how the two functions are...

  • You are to write two functions, printString() and testString(), which are called from the main function....

    You are to write two functions, printString() and testString(), which are called from the main function. printString (string) prints characters to std::cout with a space after every character and a newline at the end. testString (string) returns true if the string contains two consecutive characters that are the same, false otherwise. See the main() to see how the two functions are called. Some sample runs are given below: string: “hello” printString prints: h e l l o testString returns: true...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • CODE THE FOLLOWING FUNCTIONS IN JAVASCRIPT (USE OF THE ABOVE MENTIONED IN BUILT FUNCTIONS IS NOT...

    CODE THE FOLLOWING FUNCTIONS IN JAVASCRIPT (USE OF THE ABOVE MENTIONED IN BUILT FUNCTIONS IS NOT ALLOWED) Write a function that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Write a function that accepts two arguments (a...

  • roblem description Write the following functions as prototyped below. Do not alter the function signatures. Demonstrate...

    roblem description Write the following functions as prototyped below. Do not alter the function signatures. Demonstrate each function using literal strings defined in your client program (e.g., don't prompt the user for test inputs) /// Returns a copy of the string with its first character capitalized //I and the rest lowercased /// Example: capitalize("hELLO WORLD") returns "Hello world std::string capitalize(const std::string&str) /// Returns a copy of the string centered in a string of length ·width.. /// Padding is done using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT