Pages

Friday, 19 August 2016

Chapter 5 Exercise 48, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

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

    Enter a string: Beijing Chicago
    BiigCiao

import java.util.Scanner;
 
public class ProgrammingEx5_48 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.print("Enter a string:");
  String s = input.nextLine();
 
  for (int i = 0; i < s.length(); i++) {
   if ((i + 1) % 2 == 1) {
    System.out.print(s.charAt(i));
   }
  }
 }
}

No comments:

Post a Comment