I'm stuck on a class to create a program to solve a question. I have a main method and a secondary test method which is working in combination to solve this problem, although when there is a change, I can not get the solution to work. The problem is making sure that a word square is free, here is an excerpt from the problem:
For this part, apply a method called SquareFree which is a form of input Takes in (a reference) an array of characters. You can assume that elements of the array are all lowercase letters. (In other words, you do not need to worry about any question: "Is Z is a letter similar to Z?") Your method should test whether the given input array is class-free or not. If so, then the method should be printed by stating that the message should be printed otherwise, that the world is not wide-open, where the class sounding begins and what is the nickname for example , If the word is zatabracabrac in the given array, then the method should be printed: word, vbracrack, word is not free, because it has the syllabus, abrac when the word position starts from 2 times.
I have the current code below, it works in this case that there are repeat characters in front of each other, but I do not know how many repeats Characters (for example abab) and I'm not sure how to print the repeating syllable. Main scanner keyboard = new scanner (System.in)
public static zero main (string [] arg) {// part (a)); Println ("*****************"); System.out.println ("Part (A)"); Println ("*****************"); Do {System.out.println ("Enter a word and then press enter:"); String str = keyboard.next (); Four [] word = str.toCharArray (); IsSquareFree (word); System.out.println ("Do you want to check any other word? Yes, press Y, or not for other key"); } While (keyboard.next () charAt (0) == 'y'.); } Public stable is zero SquareFree (four [] words) {int sqf = 0; For (int i = 0; i & lt; word.length; i ++) {for (int j = 0; j & lt; word.length-1; j ++) {if (word [j] = = Word [j + 1]) {Sqf = 1; J = words Long; } And {sqf = 2; }}} If (sqf == 1) {System.out.println (); System.out.println ("Square-free"); } Else {System.out.println (); System.out.println ("Square Free"); }}}
I would also like to add that I do not have permission to use the array class for this question, nor do I have permission to use the string and I do not have the main method Can not change, I can not change the input for my other method.
-
To see if the sequence of characters, the length of a sequence For repeats (say, n ), you change your
if
with a loop, which compares theword [j + x] With the
word [j + n + x]
for each value of 0 and n betweenx
; And only consider them all if all match n , you will need a loop on these n values forx
; If you need to consider the different values of n , then you will need another loop to let them know.This is not clear from your code which you are using
for
, but if it is the length of the repeating part (which I n ), then you only have to consider values up to half lengthword
(or there is no room to repeat it). -
To print a sub-word, you can print each individual letter (
println print
) > Using
Comments
Post a Comment