Q:

(Process string) Write a program that prompts the user to enter a string and displays the characters at odd positions. Here is a sample run:

0

(Process string) Write a program that prompts the user to enter a string and displays the characters at odd positions. Here is a sample run:

Output:

Enter a string: Beijing Chicago
BiigCiao

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*
(Process string) Write a program that prompts the user to enter a string and displays
the characters at odd positions.
*/
import java.util.Scanner;

public class Exercise_05_48 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		// Prompt the user to enter a string
		System.out.print("Enter a string: ");
		String string = input.nextLine();

		String oddPositions = "";	// Hold the characters at odd positions

		// Concat that characters in the string at odd positions
		for (int i = 0; i < string.length(); i+=2) {
			oddPositions += string.charAt(i);
		}

		// Display results
		System.out.println(oddPositions);
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now