Question

Write a Java method that takes a String as its input and prints on the first...

Write a Java method that takes a String as its input and prints on the first line the odd characters (1st, 3rd, etc) and on the second line the even characters (2nd, 4th, etc).

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

import java.util.*;

class EvenOddChars
{
public static void main (String[] args)
{
  Scanner input = new Scanner(System.in);
  
  //input the string
  System.out.println("Enter the string : ");
  String str = input.next();
  
  evenOddChars(str);
  
  
}

public static void evenOddChars(String str)
{
  //display odd characters
  System.out.println("\nOdd Characters : ");
  for(int i=1;i<str.length();i+=2)
  System.out.print(str.charAt(i)+" ");
  
  System.out.println();
  
  System.out.println("\nEven Characters : ");
  //display even characters
  for(int i=0;i<str.length();i+=2)
  System.out.print(str.charAt(i)+" ");
}
}

output:

Enter the string :abcdefghijklmnop

Odd Characters :
b d f h j l n p

Even Characters :
a c e g i k m o

Add a comment
Know the answer?
Add Answer to:
Write a Java method that takes a String as its input and prints on the first...
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
  • 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 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....

  • IN PYTHON Implement a function easyCrypto() that takes as input a string and prints its encryption...

    IN PYTHON Implement a function easyCrypto() that takes as input a string and prints its encryption defined as follows: Every character at an odd position i in the alphabet will be encrypted with the character at position i + 1, and every character at an even position i will be encrypted with the character at position i - 1. In other words, ‘a’ is encrypted with ‘b’, ‘b’ with ‘a’, ‘c’ with ‘d’, ‘d’, with ‘c’, and so on. Lowercase...

  • write in java split() A static method that takes as input parameter a ThingArrayQueue called inputQ,...

    write in java split() A static method that takes as input parameter a ThingArrayQueue called inputQ, that includes things with positive number attributes. The method returns an array of two ThingArrayQueues as output where the first queue includes all things with even values from input and the second queue includes all things with odd values from inputQ. Zero is considered an even number. The input queue should be empty after calling this method. *Queue has a mixture of things with...

  • Write a static method called printWithSpaces that takes a String as its parameter and prints the...

    Write a static method called printWithSpaces that takes a String as its parameter and prints the characters of the string separated by spaces. For example: > Methods.printWithSpaces("method") m e t h o d You should have a single space after the last character. This method should not return a value. That is similar to this code for printing the string vertically public static void printVertical(String s) { for (int i = 0; i < s.length(); i++) { char c =...

  • Write a JAVA program that has a method which accepts a string and prints it back...

    Write a JAVA program that has a method which accepts a string and prints it back to the user with all vowels replaced with *'s (multiple asterisks not '*s') with a new line after the string. The method signature should look like this: public static void replacePrint(String input)

  • Write a static method called encodeString that takes a string as input and prints to the...

    Write a static method called encodeString that takes a string as input and prints to the console a word where each letter is advanced by one value. In addition, your method should return the encoded String. You may make use of the method provided below to help you. public static char encode(char x) { int current = x; current++; return (char)current; } This method takes a character and returns an "encoded" character. In other words, encode('a') will return 'b'. Your...

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

  • Write a JAVA program that has 2 methods with the same name: printCaps(String input) prints the...

    Write a JAVA program that has 2 methods with the same name: printCaps(String input) prints the input in capital letters, printCaps(String input, int num) prints in capital letters the specified number of times on a new line: public static void printCaps(String input) public static void printCaps(String input, int num)

  • C Program: Write the following function that takes a user input string, computes, and determines whether the input is palindromic or not by returning either true or false:

    A palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward or reversed. Examples: “Dad”, “Anna”, “Never odd or even”, etc. For this problem, we will only consider a string to be palindromic if its combined alpha-numeric characters (‘A’-’Z’, ‘a’-’z’, and ‘0’-’9’) can be read the same both forward and backward, by skipping all other non-alphanumeric characters.Write the following function that takes a user input string, computes, and determines whether the input is palindromic or not...

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