Question

In: Computer Science

/**    * Returns the string formed by alternating the case of the characters in   ...

/**
   * Returns the string formed by alternating the case of the characters in
   * the specified string. The first character in the returned string is in
   * lowercase, the second character is in uppercase, the third character is
   * in lowercase, the fourth character is in uppercase, and so on.
   * Examples:
   *
   * <ul>
   * <li><code>alternatingCaps("a")</code> returns <code>"a"</code>
   * <li><code>alternatingCaps("ab")</code> returns <code>"aB"</code>
   * <li><code>alternatingCaps("abc")</code> returns <code>"aBc"</code>
   * <li><code>alternatingCaps("XYZ")</code> returns <code>"xYz"</code>
   * <li><code>alternatingCaps("Toronto")</code> returns <code>"tOrOnTo"</code>
   * <li><code>alternatingCaps("eecs2030")</code> returns <code>"eEcS2030"</code>
   * </ul>
   *
   * <p>
   * The conversion of characters to lower or uppercase is identical to
   * that performed by the methods <code>Character.toLowerCase(int)</code>
   * and <code>Character.toLowerCase(int)</code>
   *
   * @param s
   *            a string
   * @return the string formed by alternating the case of the characters in s
   */
   public static String alternatingCaps(String s) {
       return "";
   }

Solutions

Expert Solution

public class AlternatingCaps {

    /**
     * Returns the string formed by alternating the case of the characters in
     * the specified string. The first character in the returned string is in
     * lowercase, the second character is in uppercase, the third character is
     * in lowercase, the fourth character is in uppercase, and so on.
     * Examples:
     *
     * <ul>
     * <li><code>alternatingCaps("a")</code> returns <code>"a"</code>
     * <li><code>alternatingCaps("ab")</code> returns <code>"aB"</code>
     * <li><code>alternatingCaps("abc")</code> returns <code>"aBc"</code>
     * <li><code>alternatingCaps("XYZ")</code> returns <code>"xYz"</code>
     * <li><code>alternatingCaps("Toronto")</code> returns <code>"tOrOnTo"</code>
     * <li><code>alternatingCaps("eecs2030")</code> returns <code>"eEcS2030"</code>
     * </ul>
     *
     * <p>
     * The conversion of characters to lower or uppercase is identical to
     * that performed by the methods <code>Character.toLowerCase(int)</code>
     * and <code>Character.toLowerCase(int)</code>
     *
     * @param s a string
     * @return the string formed by alternating the case of the characters in s
     */
    public static String alternatingCaps(String s) {
        String result = "";
        for (int i = 0; i < s.length(); i++) {
            if (i % 2 == 0) {
                result += Character.toLowerCase(s.charAt(i));
            } else {
                result += Character.toUpperCase(s.charAt(i));
            }
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(alternatingCaps("a"));
        System.out.println(alternatingCaps("ab"));
        System.out.println(alternatingCaps("abc"));
        System.out.println(alternatingCaps("XYZ"));
        System.out.println(alternatingCaps("Toronto"));
        System.out.println(alternatingCaps("eecs2030"));
    }
}

Related Solutions

Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
write a python code that Returns a string composed of characters drawn, in strict alternation, from...
write a python code that Returns a string composed of characters drawn, in strict alternation, from s1 and s2. If one string is longer than the other, the excess characters are added to the end of the string as shown in the examples below #Example 1 - blend("ape", "BANANA") returns "aBpAeNANA" #Example 2 - blend("BOOT", "gold") returns "BgOoOlTd"
Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
A palindrome is a string of characters (a word, phrase, or sentence) that is the same...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward – assuming that you ignore spaces, punctuation and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. 1. Describe how you could use a stack to test whether a string is a palindrome. 2. Describe how you could use a queue to test whether a string is...
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced.
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced. Grouping characters are ( and ), [ and ], and { and }. I got the basic idea for this problem from HackerRank. It’s also a very common interview question.*******************8Example output**************Enter the expression:{[()]} {[()]}is balancedEnter the expression: {()()} {()()}is balancedEnter the expression: {([[])} {([[])}is not balancedEnter the expression: {([)]} {([)]}is not balanced
A DNA strand is represented by a string of the characters A, C, G, and T,...
A DNA strand is represented by a string of the characters A, C, G, and T, each of which represents a nucleotide. Each nucleotide has its complement as indicated by this Ruby hash: NUCLEOTIDE_COMPLEMENT = { 'A' => 'T', 'T' => 'A', 'C' => 'G', 'G' => 'C' } The reverse-complement of a DNA string is a new string in which each nucleotide is replaced by its complement and the string is reversed. Reverse-complements are important in bioinformatics since a...
This function takes in a string as a parameter and prints the average number of characters...
This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision (see test cases below). Assume a sentence always ends with a period (.) or when the string ends. Assume there is always a blank space character (" ") between each word. Do not count the blank spaces between words or the periods...
Suppose you are given a string containing only the characters ( and ). In this problem,...
Suppose you are given a string containing only the characters ( and ). In this problem, you will write a function to determine whether the string has balanced parentheses. Your algorithm should use no more than O (1) space beyond the input; any algorithms that use space not in O (1) will receive half credit at most. Any solutions that always return true (or always return false) or otherwise try to game the distribution of test cases will receive zero...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT