Rotate a given String layout clockwise - java

Given a String layout of the following form:
X......X
....X..X
....X..X
Rotate the above layout by 90 degrees clockwise which should be:
..X
...
...
...
XX.
...
...
XXX
What's the easiest way to rotate the String characters clockwise by 90 degrees? The String layout can be of any form and any size. What if I have 100000x100000 size String layout?
public String rotate(String layout)
or
public void rotate(String layout)

Edit
I fixed the mistake as pointed out by the OP in the comments below. This should produce exactly what was required in the original question above.
public static String rotateStringMatrixBy90(String matrix) {
int numberOfRows = 3; // this I leave as an exercise
int numberOfColumns = 8; // same with this one
String newMatrix = "";
int count = 0;
String[] newMatrixColumns= matrix.split("\n");
while (count < matrix.split("\n")[0].length()) {
for (int i = newMatrixColumns.length - 1; i > -1; i--) {
newMatrix = newMatrix + newMatrixColumns[i].charAt(count);
}
newMatrix = newMatrix + "\n";
count++;
}
return newMatrix;
}
And this is how you would use it:
String m = "X......X\n" +
"....X..X\n" +
"....X..X";
System.out.println(m);
m = rotateStringMatrixBy90(m);
System.out.println(m);
(Note: this assumes your using \n as the separator between the rows):

public static String[] rotate(String[] originalArray) {
String[] rotatedArray = new String[originalArray[0].length()];
for (int i=0;i<rotatedArray.length;i++) {
rotatedArray[i]="";
}
for (int j = 0; j < originalArray[0].length(); j++) {
for (int i = originalArray.length - 1; i >= 0; i--) {
rotatedArray[j] += originalArray[i].charAt(j);
}
}
return rotatedArray;
}

Edit: just saw that you wanted a String as the arg
You could use this:
public class SO {
public static void main(String[] args) throws Exception {
String string = "X......X\n" +
"....X..X\n" +
"....X..X\n";
System.out.println(string);
string = rotateClockwise(string);
System.out.println(string);
}
static String rotateClockwise(String input) {
String[] arr = input.split("\n");
int length = arr[0].length();
String[] ret = new String[length];
for (int i = 0; i < ret.length; i++) {
ret[i] = "";
}
for (int i = arr.length-1; i >= 0; i--) {
char[] chars = arr[i].toCharArray();
for (int j = 0; j < ret.length; j++) {
ret[j] += chars[j];
}
}
String output = "";
for (String str: ret)
output += str + "\n";
return output;
}
}
Please note this has NO error checking.

Related

Most basic way to insert a character into a string in Java?

Say I have a string x and I want to add some character x amount of times so that string x becomes of length y, how would I do this?
String x = "this_is_a_line";
//x.length() = 14;
length y = 20;
//insert character into line so String x becomes balanced to:
x = "this___is___a___line";
//x.length() = 20;
Another example:
String y = "in_it_yeah";
//y.length() = 10
length j = 15;
//inserting characters so String y becomes balanced to:
y = "in____it___yeah";
I want to avoid using StringBuilder to append characters.
My thought process:
Create a Char array of length y.
Copy the string to array.
Attempt to shift the characters one by one
from the furthest character to the right.
I hope I understood you correctly but I think this code does what you're requesting.
Edit: this one will distribute the specified character evenly and can create strings like "a__b_c" if the string isn't long enough yet.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
String string = "this_is_a_line";
int length = string.length();
int desiredLength = 16;
int extraLengthRequired = desiredLength - length;
char characterToBeDuplicated = '_';
String[] rawPieces = string.split(Character.toString(characterToBeDuplicated));
ArrayList<String> pieces = new ArrayList<>();
int emptyIndexes = 0;
for (String piece : rawPieces) {
if(piece.equals("")) {
emptyIndexes++;
} else {
pieces.add(piece);
}
}
int numOfCharactersToBeMultiplied = pieces.size() - 1;
int numOfMultiplications = (int) Math.floor((extraLengthRequired + emptyIndexes) / numOfCharactersToBeMultiplied) + 1;
int lengthLeft = (extraLengthRequired + emptyIndexes) % numOfCharactersToBeMultiplied;
String newString = pieces.get(0);
for (int i = 1; i < pieces.size(); i++) {
newString += characterToBeDuplicated;
if(lengthLeft > 0) {
newString += characterToBeDuplicated;
lengthLeft--;
}
for (int j = 1; j < numOfMultiplications; j++) {
newString += characterToBeDuplicated;
}
newString += pieces.get(i);
}
System.out.println(newString + " - " + newString.length());
}
}
Old solution:
public class Main {
public static void main(String[] args) {
String string = "this_is_a_line";
int length = string.length();
int desiredLength = 20;
int extraLengthRequired = desiredLength - length;
char characterToBeDuplicated = '_';
String[] pieces = string.split(Character.toString(characterToBeDuplicated));
int numOfCharactersToBeMultiplied = pieces.length - 1;
int numOfMultiplications = (int) Math.floor(extraLengthRequired / numOfCharactersToBeMultiplied);
String newString = pieces[0];
for (int i = 1; i < pieces.length; i++) {
newString += characterToBeDuplicated;
for (int j = 1; j < numOfMultiplications; j++) {
newString += characterToBeDuplicated;
}
newString += pieces[i];
}
System.out.println(newString);
}
}
I did this one because when I started doing a pseudo code it was a bit challenging for me, though I'm not a fan of answering questions like this on "gimme teh codez".
I recommend you though using StringBuilder on String concatenation inside for loops because it's more efficient than actual String concatenation with +=.
Note that:
This program adds spaces to the end, that's why I print it's length trimmed.
I split it by space with a regex \\s+ instead of _ because that's how you wrote it 1st
The following code works for
a____b_c -> a___b___c (Lenght 9)
a____b_c -> a____b___c (Lenght 10)
this_is_a_line -> this___is___a___line (Lenght 20)
in_it_yeah -> in____it___yeah (Length 15)
Code:
class EnlargeString {
public static void main (String args[]) {
String x = "a b c";
int larger = 10;
int numberOfSpaces = 0;
String s[] = x.split("\\s+"); //We get the number of words
numberOfSpaces = larger - s.length; //The number of spaces to be added after each token
int extraSpaces = numberOfSpaces % s.length; //Extra spaces for the cases of 4 spaces then 3 or something like that
System.out.println(extraSpaces);
String newSpace[] = new String[s.length]; //The String array that will contain all string between words
for (int i = 0; i < s.length; i++) {
newSpace[i] = " "; //Initialize the previous array
}
//Here we add the extra spaces (the cases of 4 and 3)
for (int i = 0; i < s.length; i++) {
if (extraSpaces == 0) {
break;
}
newSpace[i] += " ";
extraSpaces--;
}
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < totalSpaces / s.length; j++) {
newSpace[i] += " "; //Here we add all the equal spaces for all tokens
}
}
String finalWord = "";
for (int i = 0; i < s.length; i++) {
finalWord += (s[i] + newSpace[i]); //Concatenate
}
System.out.println(x);
System.out.println(x.length());
System.out.println(finalWord);
System.out.println(finalWord.trim().length());
}
}
Next time try to do it yourself 1st and show YOUR logic, then your question will be better received (maybe with upvotes instead of downvotes) this is called a Runnable Example. I also recommend you to take a look at String concatenation vs StringBuilder and How to ask a good question, also you might want to Take the tour
Here is my solution:
public class Main
{
public static void main(String args[]){
System.out.print("#Enter width : " );
int width = BIO.getInt();
System.out.print("#Enter line of text : " );
String line = BIO.getString().trim();
int nGaps, spToAdd, gapsLeft, modLeft, rem;
nGaps = spToAdd = gapsLeft = rem = 0;
double route = 0;
String sp = " ";
while ( ! line.equals( "END" )){
nGaps = numGaps(line);
if (nGaps == 0) { line = compLine(line, width).replace(" ", "."); }
else if (nGaps == width) { line = line.replace(" ", "."); }
else{
int posArray[] = new int[nGaps];
posArray = pos(line, nGaps);
gapsLeft = width - line.length();
spToAdd = gapsLeft / nGaps;
modLeft = gapsLeft % nGaps;
route = gapsLeft / nGaps;
sp = spGen(spToAdd);
line = reFormat(posArray, line, width, sp, spToAdd);
if (line.length() < width){
System.out.print("#OK\n");
nGaps = numGaps(line);
int posArray2[] = new int[nGaps];
posArray2 = pos(line, nGaps);
line = compFormat(posArray2, line, modLeft);
}
line = line.replace(" ", ".");
}
System.out.println(line);
System.out.println("#Length is: " + line.length());
System.out.print("#Enter line of text : " );
line = BIO.getString().trim();
}
}
public static int numGaps(String oLine){
int numGaps = 0;
for (char c : oLine.toCharArray()) { if (c == ' ') { numGaps++; } }
return numGaps;
}
public static String spGen(int count) {
return new String(new char[count]).replace("\0", " ");
}
public static String compLine(String oLine, int width){
String newLine = oLine;
int pos = oLine.length();
int numOSpace = width - oLine.length();
String sp = spGen(numOSpace);
newLine = new StringBuilder(newLine).insert(pos, sp).toString();
return newLine;
}
public static int[] pos(String oLine, int nGaps){
int posArray[] = new int[nGaps];
int i = 0;
for (int pos = 0; pos < oLine.length(); pos++) {
if (oLine.charAt(pos) == ' ') { posArray[i] = pos; i++; }
}
//for (int y = 0; y < x; ++y) { System.out.println(posArray[y]); }
return posArray;
}
public static String reFormat(int[] posArray, String oLine, int width, String sp, int spToAdd){
String newLine = oLine;
int mark = 0;
for (int i = 0; i < posArray.length; ++i){ /*insert string at mark, shift next element by the num of elements inserted*/
if (newLine.length() > width) { System.out.println("Maths is wrong: ERROR"); System.exit(1);}
else { newLine = new StringBuilder(newLine).insert(posArray[i]+mark, sp).toString(); mark += spToAdd; }
}
return newLine;
}
public static String compFormat(int[] posArray2, String mLine, int modLeft){
String newLine = mLine;
int mark = 0;
for (int i = 0; i < modLeft; ++i){
//positions
//if position y is != y+1 insert sp modLeft times
if (posArray2[i] != posArray2[i+1] && posArray2[i] != posArray2[posArray2.length - 1]){
newLine = new StringBuilder(newLine).insert(posArray2[i]+mark, " ").toString(); mark++;
}
}
return newLine;
}
}

drawing Diamond of numbers with 2D array in java

So I need to make a diamond_look of numbers using 2D array in Java. I got my results but with null before the diamond. For drawNumDiamond(9) I have to get a diamond look that goes until 5 and back. I know I can make it without using array, but I want to learn more about 2D arrays :this is how it should look like and what are my results
public class Example1{
private static void drawNumDiamond(int h) {
if(h%2 != 0) {
int size = h/2 +1;
int count = 1;
int loop = 1;
String[][] dijamant = new String[h][];
for(int row = 0; row < dijamant.length; row++) {
dijamant[row] = new String[row+1];
for(int kolona=0; kolona<=row; kolona++) {
dijamant[0][0] = "1";
for(int i=0; i< loop;i++) {
dijamant[row][kolona]+= count;
}
}
count++;
loop+=2;
}
for (int k = 0; k < size; k++) {
System.out.printf("%" + h + "s", dijamant[k]);
h++;
System.out.println();
}
h--;
for (int q = size - 2; q>=0; q--) {
h--;
System.out.printf("%" + h + "s", dijamant[q]);
System.out.println();
}
}
}
public static void main(String[] args) {
drawNumDiamond(9);
}
}
The issue is in this line :
dijamant[row][kolona] += count;
if dijamant[row][kolona] is null and count is 2, the result of the string concatenation will be "null2". Try adding the following if statement before to initialize with an empty string :
if (dijamant[row][kolona] == null) {
dijamant[row][kolona] = "";
}
This will get your code working, but there are still things to think about. E.g. you keep setting dijamant[0][0] = "1"; in the loop.

a method that recieves String input and divides it into 1,2 and 3 different sections

i need a more basic method! that will just split words into 1,2,3 consecutive compartments, my output for "water" should be in terms of array string! e.g INPUT of water should produce OUTPUT like {"w","a","t","e","r","wa","at","te","er","wat","ate","ter"}
public void split(String word) {
int x=(word.length())/2;
int z=word.length();
String[] partition= new String[5];
for (int i=0; i<x; i++){
String new1=Character.toString(word.charAt(i));
String new2 = Character.toString(word.charAt(i))+Character.toString(word.charAt(i+1));
String new3 = Character.toString(word.charAt(i))+Character.toString(word.charAt(i+1))+Character.toString(word.charAt(i+2));
String new4 = removeDuplicate(Character.toString(word.charAt(z-2))+ Character.toString(word.charAt(z-1)));
String new5 = removeDuplicate(Character.toString(word.charAt(z-3))+ Character.toString(word.charAt(z-2))+ Character.toString(word.charAt(z-1)));
char result = word.charAt(2);
String[] partitions = {new1,new5,new3,new4,new2};
//partition=partitions;
//Arrays.toString((removeDuplicates(partitions)))
System.out.println(partitions);
}
//return partition;
}
Here is an example how you can do that. I hope if you are not a beginner you know about ArrayList.
String word = "water";
ArrayList<String> list = new ArrayList<String>();
for (int i = 1; i <= 3; i++) {
int limit = word.length() - i + 1;
for (int j = 0; j < limit; j++) {
list.add(word.substring(j, j+i));
}
}
System.out.println(list);
Output :
[w, a, t, e, r, wa, at, te, er, wat, ate, ter]
Try to understand it. and tell me if having any doubt.
public String[] foo(String word){
String[] consec = new String[3*word.length() - 3];
String substr;
int k = 0;
for(int i = 1; i <= 3; i++){
for(int j = 0; j <= word.length() - i; j++){
substr = word.substring(j, j+i);
consec[k] = substr;
k++;
}
}
return consec;
}

Alternating characters of two different inputs

I want to take two strings and alternate the characters into a new string using a for method.
Example: "two" and "one"
Result: "townoe"
This is what I have so far, and I really don't know how to finish it.
public class Alternator {
String alternate(String a, String b) {
String s = "";
for (int i = 0; i < a.length(); i++) {
s += i;
System.out.println(s);
}
return null;
}
}
public class Alternator{
public static String alternate(String a, String b){
String s = "";
int i = 0;
while (i < a.length() && i < b.length()){
s += a.charAt(i) +""+ b.charAt(i);
i++;
}
while (i < a.length() ){
s += a.charAt(i);
i++;
}
while (i < b.length()){
s += b.charAt(i);
i++;
}
return s;
}
public static void main(String[] args){
String a = "two", b = "one";
String s = Alternator.alternate(a,b);
System.out.println(s);
}
}
To use for loop instead of while loop, simply remove all while lines with for lines like the following, then remove the i++ line from each while loop
for(; i < a.length() && i < b.length(); i++){
//the inside of the loop MINUS THE LINE i++
}
for(; i < a.length(); i++){
//the inside of the loop MINUS THE LINE i++
}
for(; i < b.length(); i++){
//the inside of the loop MINUS THE LINE i++
}
Here is some compact way of doing that:
String alternate(String a, String b) {
StringBuilder builder = new StringBuilder();
int smallerStringLength = Math.min(a.length(), b.length());
for (int i = 0; i < smallerStringLength; i++) {
builder.append(a.charAt(i));
builder.append(b.charAt(i));
}
return builder.toString();
}
Or even more optimized:
String alternate(String first, String second) {
char[] firstChars = first.toCharArray();
char[] secondChars = second.toCharArray();
int smallerCharsCount = Math.min(firstChars.length, secondChars.length);
StringBuilder builder = new StringBuilder(smallerCharsCount * 2);
for (int i = 0; i < smallerCharsCount; i++) {
builder.append(firstChars[i]);
builder.append(secondChars[i]);
}
return builder.toString();
}
This will work if string are of same length or of the different lengths.
static void mergeStrings(String a, String b) {
StringBuilder mergedBuilder = new StringBuilder();
char[] aCharArr = a.toCharArray();
char[] bCharArr = b.toCharArray();
int minLength = aCharArr.length >= bCharArr.length ? bCharArr.length : aCharArr.length;
for (int i=0; i<minLength; i++) {
mergedBuilder.append(aCharArr[i]).append(bCharArr[i]);
}
if(minLength < aCharArr.length) {
mergedBuilder.append(a.substring(minLength));
}
else{
mergedBuilder.append(b.substring(minLength));
}
Systemout.println(mergedBuilder.toString());
}
Assuming that the two strings are the exact same length, you can do the following. If they are different length, then currently your prompt doesn't say how you want the resultant string to be set up.
public class Alternator {
String alternate(String a, String b) {
String s = "";
for (int i = 0; i < 2*a.length(); i++) {
if (i%2==0) // modular arithmetic to alternate
s += a.charAt(i/2); // Note the integer division
else
s += b.charAt(i/2);
}
System.out.println(s);
return s;
}
}
Alternatively, even easier, but the index i doesn't mark the length of your string s:
public class Alternator {
String alternate(String a, String b) {
String s = "";
for(int i = 0; i < a.length(); i++){
s += a.charAt(i);
s += b.charAt(i);
}
return s;
}
}
Use this:
String alternate(String a, String b){
StringBuilder builder = new StringBuilder();
final int greaterLength = a.length() > b.length() ? a.length() : b.length();
for(int i = 0; i < greaterLength; i++){
if (i < a.length()) {
builder.append(a.charAt(i));
}
if (i < b.length()) {
builder.append(b.charAt(i));
}
}
return builder.toString();
}
It uses the String.charAt method to obtain letters, and a StringBuilder to create the string.
(When given two strings of non-equal length, this returns an alternation of the first two chars, and then does just the remaining string. EG: Hello and Hi --> HHeillo)
According to the comments I've read, you are having trouble understanding for loops, and how to use them with strings.
For loops are most often used to iterate over arrays, or to perform a task a given number of times.
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
This would give the output
0
1
2
3
4
For loops start at the value of the initializer, the first thing you put in int i = 0;
They then check the expression, the second part of the for loop, and if it returns true, it executes all of the code inside the braces. i < 5;
Once it has done that, it runs the incrementor, the last part of the for loop. i++
After that, it checks the expression again. I guess you can see where this is going. Until the expression returns false, everything inside the curly braces of the for loop gets executed over and over again.
Strings can be iterated over with a for loop, but you can't reference it like an array using array[index]. You have to either convert it into an array, using .toCharArray() on your String, and return the result to an empty char array char[], or use the .charAt(index) method on your string.
This code will go over a string, and output each character, one by one:
for (int i = 0; i < myString.length(); i++) {
System.out.println(myString.charAt(i));
}
If the string had a value of "Hello", the output would be:
H
e
l
l
o
Using this, instead of outputting the characters using System.out.println();, we can put them into an empty string, using +=:
myOtherString += myString.charAt(i);
That means, if we want to go over two Strings at a time, and alternate them, like you do, we can iterate over two strings at the same time, and add them to a new string:
myAlternatedString += myString.charAt(i);
myAlternatedString += myOtherString.charAt(i);
if MyString was still "Hello" and myOtherString was "World", the new string would be:
Hweolrllod
following code reads 2 different inputs and merges into a single string.
public class PrintAlternnateCharacterString {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = in.next();
String b = in.next();
String mergedString = "";
int lenA = a.length();
int lenB = b.length();
if (lenA >= lenB) {
for (int i = 0; i < lenA; i++) {
if (i < lenB) {
mergedString += a.charAt(i) + "" + b.charAt(i);
} else {
mergedString += a.charAt(i);
}
}
}
if (lenB > lenA) {
for (int i = 0; i < lenB; i++) {
if (i < lenA) {
mergedString += a.charAt(i) + "" + b.charAt(i);
} else {
mergedString += b.charAt(i);
}
}
}
System.out.println("the merged string is-->" + mergedString);
}
}
public static String stringConcate(String str1,String str2){
String str3="";
if(str1!=null && str2!=null && !str1.isEmpty() && !str2.isEmpty()){
if(str1.length()==str2.length()){
for(int i=0;i<=str1.length()-1;i++){
str3+=str1.charAt(i);
str3+=str2.charAt(i);
}
}
if(str1.length()>str2.length()){
for(int i=0;i<=str1.length()-1;i++){
str3+=str1.charAt(i);
if(i<str2.length()){
str3+=str2.charAt(i);
}
}
}
if(str2.length()>str1.length()){
for(int i=0;i<=str2.length()-1;i++){
if(i<str1.length()){
str3+=str1.charAt(i);
}
str3+=str2.charAt(i);
}
}
}
return str3;
}
String str1 = "one"; String str2 = "two";
StringBuilder sb = new StringBuilder();
int i = 0;
for (; i < str1.length() && i < str2.length(); i++) {
sb.append(str1.charAt(i)).append(str2.charAt(i));
}
for(; i < str1.length(); i++) {
sb.append(str1.charAt(i));
}
for(; i < str2.length(); i++) {
sb.append(str2.charAt(i));
}
System.out.println("result = " + sb.toString());// otnweo
This will handle for different length too
This could be donw with very simple if...else.
public static void main(String... args) {
int[] one = { 1, 2, 3 };
int[] two = { 44, 55, 66, 77, 88 };
System.out.println(Arrays.toString(alternate(one, two)));
}
public static int[] alternate(int[] one, int[] two) {
int[] res = new int[one.length + two.length];
for (int i = 0, j = 0, k = 0; i < res.length; i++) {
if (i % 2 == 0)
res[i] = j < one.length ? one[j++] : two[k++];
else
res[i] = k < two.length ? two[k++] : one[j++];
}
return res;
}
Output:
[1, 44, 2, 55, 3, 66, 77, 88]

Modifying Boyer Moore for multiple pattern search

I have used Boyer Moore algorithm according to this site. This implements pattern search in the text for only once and the program exits. Can anyone help me to modify this code in order to find the pattern multiple times with their starting and ending index?
public class BoyerMoore {
private final int R; // the radix
private int[] right; // the bad-character skip array
private String pat; // or as a string
// pattern provided as a string
public BoyerMoore(String pat) {
this.R = 256;
this.pat = pat;
// position of rightmost occurrence of c in the pattern
right = new int[R];
for (int c = 0; c < R; c++)
right[c] = -1;
for (int j = 0; j < pat.length(); j++)
right[pat.charAt(j)] = j;
}
// return offset of first match; N if no match
public ArrayList<Integer> search(String txt) {
int M = pat.length();
int N = txt.length();
ArrayList<Integer> newArrayInt = new ArrayList<Integer>();
int skip;
for (int i = 0; i <= N - M; i += skip) {
skip = 0;
for (int j = M-1; j >= 0; j--) {
if (pat.charAt(j) != txt.charAt(i+j)) {
skip = Math.max(1, j - right[txt.charAt(i+j)]);
break;
}
}
if (skip == 0)
newArrayInt.add(i); // found
}
return newArrayInt; // not found
}
// test client
public static void main(String[] args) {
String pat = "abc";
String txt = "asdf ghjk klll abc qwerty abc and poaslf abc";
BoyerMoore boyermoore1 = new BoyerMoore(pat);
ArrayList<Integer> offset = boyermoore1.search(txt);
// print results
System.out.println("Offset: "+ offset);
}
}
I got it. The skip was always 0 when it found the pattern in the text.
public class BoyerMoore {
private final int R; // the radix
private int[] right; // the bad-character skip array
private String pat; // or as a string
// pattern provided as a string
public BoyerMoore(String pat) {
this.R = 256;
this.pat = pat;
// position of rightmost occurrence of c in the pattern
right = new int[R];
for (int c = 0; c < R; c++)
right[c] = -1;
for (int j = 0; j < pat.length(); j++)
right[pat.charAt(j)] = j;
}
// return offset of first match; N if no match
public ArrayList<Integer> search(String txt) {
int M = pat.length();
int N = txt.length();
ArrayList<Integer> newArrayInt = new ArrayList<Integer>();
int skip;
for (int i = 0; i <= N - M; i += skip) {
skip = 0;
for (int j = M-1; j >= 0; j--) {
if (pat.charAt(j) != txt.charAt(i+j)) {
skip = Math.max(1, j - right[txt.charAt(i+j)]);
break;
}
}
if (skip == 0)
{
newArrayInt.add(i); // found
skip++;
}
}
return newArrayInt; // not found
}
// test client
public static void main(String[] args) {
String pat = "abc";
String txt = "asdf ghjk klll abc qwerty abc and poaslf abc";
BoyerMoore boyermoore1 = new BoyerMoore(pat);
ArrayList<Integer> offset = boyermoore1.search(txt);
// print results
System.out.println("Offset: "+ offset);
}
}
public class Boyer {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner get= new Scanner(System.in);
String m,n;
int i,j;
String T,P;
T=get.nextLine();
System.out.println("Text T is"+T);
P=get.nextLine();
System.out.println("Pattern P is"+P);
int n1=T.length();
int m1=P.length();
for(i=0;i<=n1-m1;i++){
j=0;
while(j<m1 && (T.charAt(i+j)==P.charAt(j))){
j=j+1;
if(j==m1)
System.out.println("match found at"+i);
}
}
}
}

Categories