Question

Write a Java program which takes a string as a user input. Create 2 functions. The...

Write a Java program which takes a string as a user input. Create 2 functions.

The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console.

The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string. The function then prints out the character to screen.

Example:

<output>

Enter a string: Hello World <enter icon>

Convert string: hELLO wORLD

The first character that duplicate 2 times is: o

<output>

<output>

Enter a string: Welcome to JaVa Class <enter icon>

Convert string: wELCOME TO jAvA cLASS

The first character that duplicate 2 times is: e

<output>

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class StringHelp {

    public static void convertString(String s) {
        System.out.print("Convert string: ");
        for(int i = 0; i < s.length(); ++i) {
            if(Character.isUpperCase(s.charAt(i))) {
                System.out.print(Character.toLowerCase(s.charAt(i)));
            } else if(Character.isLowerCase(s.charAt(i))) {
                System.out.print(Character.toUpperCase(s.charAt(i)));
            } else {
                System.out.print(s.charAt(i));
            }
        }
        System.out.println();
    }

    public static void duplicate(String s) {
        int count;
        for(int i = 0; i < s.length(); ++i) {
            count = 0;
            for (int j = 0; j < s.length(); ++j) {
                if(s.charAt(i) == s.charAt(j)) ++count;
            }
            if(count == 2) {
                System.out.println("The first character that duplicate 2 times is: " + s.charAt(i));
                break;
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = in.nextLine();
        convertString(str);
        duplicate(str);
    }

}

Enter a string: Convert string: hELLO wORLD The first character that duplicate 2 times is: o Process finished with exit code 0

\color{blue}Hey,\;Please\;let\;me\;know\;if\;you\;need\;me\;to\;make\;any\;changes. \\ I\;would\;really\;appreciate\;an\;upvote.\;Thank\;you!

Add a comment
Know the answer?
Add Answer to:
Write a Java program which takes a string as a user input. Create 2 functions. 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
  • NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of...

    NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of EXACTLY 4 hex digits, no more, no less, converts the hexadecimal value to its decimal value using string, character parsing, and Math methods learned in this chapter, and then prints the original string and its converted decimal value. NOTE: Hex digits may be in UPPER or lower case.

  • Must be in JAVA. Write a program that uses a Scanner to read in a String....

    Must be in JAVA. Write a program that uses a Scanner to read in a String. The program will then output a new String with all the vowels (upper and lower case) removed. See output for example output. Details Input A string composed of non-numeric characters Output The input string with the vowels removed Sample input: Welcome to Dalhousie Sample output: Dlhs nvrsty

  • Using Java write a program that takes a string input from the user and then outputs...

    Using Java write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word.

  • Write a program that prompts a user for an input string of length at least two...

    Write a program that prompts a user for an input string of length at least two and prints the string with the first half in upper case and the second half in lower case. If the length of the string is odd, the "second half" should be one character longer than the "first half". Show Hint

  • Write a method in java named isValidEmail that takes a string as input parameter, and returns...

    Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “[email protected]”, where:  user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter  domain represents a sequence of alphanumeric characters (i.e., letters...

  • In Java, write a program that prompts the user to input a sequence of characters and...

    In Java, write a program that prompts the user to input a sequence of characters and outputs the number of vowels. You will need to write a method to return a value called isAVowel which will return true if a given character is a vowel and returns false if the character is not a vowel. A second method, called isAConstant, should return true if a given character is a constant and return false if the character is not a constant....

  • 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...

  • 1 – Write a subroutine that reads a word from the user.  The subroutine gets the input...

    1 – Write a subroutine that reads a word from the user.  The subroutine gets the input from the keyboard and returns as output a string containing all the characters the user typed until either a space character is found or there are no more characters in the keyboard buffer. For example: If the user types the following input: Welcome to Disney World The first call to the subroutine should return “Welcome” The second time the subroutine is called, it returns...

  • Write a function called smallestLetter that takes in a string argument and returns a char. The...

    Write a function called smallestLetter that takes in a string argument and returns a char. The char that is returned by this function is the character in the string with the lowest ASCII integer code. For example: smallestLetter("Hello") returns ‘H’ which is code 72, and smallestLetter("Hello World") returns ‘ ’ (The space character) which is code 32

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

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
ADVERTISEMENT