AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
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 ?.



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
profile ÆŁÎĒŃ ŚPÅĆĒ - anyforum.in ÆŁÎĒŃ ŚPÅĆĒ
400
up-rate
4
down-rate

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
profile Rishi Kumar - anyforum.in Rishi Kumar
523188250042
Reply This Thread
up-rate
0
down-rate

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
profile Amar - anyforum.in Amar
0520
Reply This Thread
up-rate
1
down-rate
Comments
Hi Amar,
This will not work if you give input like aba. expected output is- ´b´.
profile Rishi Kumar - anyforum.in 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 login with facebook - anyforum.in