|
|
| Java programs |
Expert:Ruby
Could you please write the followong programs for me.. Thanks in advance
1)1. write a program that reads a string composed of 12 characters then splits it into 4 different strings each composed of three characters. Print the resulting strings each on a separate line; use printf.
2.)Write a program that reads a string s then another string p contained in s. Then form 3 strings extracted from s: the first part of s that does not contain p, the part that contains p and the last part. Print each of the three strings with its length and its position in s use printf. |
| Answers |
Hi friend,
Code to solve the problem :
Answer1 :
import java.io.*; class StringExample { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter 12 character String"); String str= br.readLine(); if(str.length()==12) { for(int i=0;i<str.length();i++) { if(i%4==0) System.out.println(str.substring(i,i+4)); } } else { System.out.println("You Enter 12 character String"); } } }
Answer2 :
import java.io.*; class StringExample1 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter First String"); String str1= br.readLine(); System.out.println("Enter second String"); String str2= br.readLine(); int index = str1.indexOf(str2);
String firstPartString = str1.substring(0,index); System.out.println("firstPartString " + firstPartString); System.out.println("firstPartString length : " + firstPartString.length());
String secondPartString = str1.substring(index,index+str2.length()); System.out.println("secondPartString " + secondPartString); System.out.println("secondPartString length : " + secondPartString.length());
String thirdPartString = str1.substring(index+str2.length()); System.out.println("thirdPartString " + thirdPartString); System.out.println("thirdPartString length : " + thirdPartString.length()); } }
Thanks
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|