AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.
Follow @anyforumin
corejava-programming: Java code of sandwich problem
Sandwich String is a string having same N characters (Wrapper text) at the beginning of string and end of string. But they are in the reverse order at the end. This string contains some text which is in between this wrapper. Now your task is to write a program to find out the this text.
corejava x 353
programming x 169
Posted On :
2018-06-07 23:23:00.0
ÆŁÎĒŃ ŚPÅĆĒ
4
0
0
4
Answers
public class Sandwich {
public static void main(String[] args) {
System.out.println(getSandwichText("adefgha"));
}
public static String getSandwichText(String sandwichString){
int length=sandwichString.length();
int pos=-1;
for(int i=0; i<length/2;i++){
if(sandwichString.charAt(i)==sandwichString.charAt(length-i-1)){
pos=i;
}else{
break;
}
}
return sandwichString.substring(pos+1, length-pos-1);
}
}
Posted On :
2018-06-15 11:12:15
Satisfied :
2 Yes
2 No
Rishi Kumar
523
1882
50042
Reply This Thread
0
import java.util.Scanner;
public class SandwichStringExample {
private static Scanner scanner = new Scanner(System.in);
private static String someText = "";
public static void main(String args[]){
System.out.println("Enter a string");
someText = scanner.next();
char textArray[] = someText.toCharArray();
int strLenght = textArray.length -1;
for(int i = 0; i < strLenght; i++){
if( textArray[i] != textArray[strLenght - i] ){
System.out.println("Sandiwch String is = "+
someText.substring(i, (strLenght - i+1) ));
break;
}
}
}
}
Posted On :
2018-07-07 08:40:11
Satisfied :
0 Yes
0 No
Amar
0
52
0
Reply This Thread
1
Comments
Hi Amar,
This will not work if you give input like aba. expected output is- ´b´.
Rishi Kumar
523
1882
50042
Posted On :
2018-07-07 16:29:50.0
Leave a Comment
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in