Jump to content

xyz middle

- - - - -

  • Please log in to reply
2 replies to this topic

#1
heartybowl

heartybowl

    Newbie

  • Members
  • PipPip
  • 17 posts
I need help with this hard problem! We are only allowed to use while loops.


import java.util.Scanner;

/**

 *  CS 170 Exercise Problem

 *  NOTE: YOU MUST USE while, NOT for for this problem.

 *

 *  @author (your name goes here)

 *  @version (place the date here)

 */

public class XyzMiddle

{

    /**

     *  Given a string, does "xyz" appear in the

     *  "middle" of the string? To define middle, we'll

     *  say that the number of chars to the left and right

     *  of the "xyz" must differ by at most one. This

     *  problem is harder than it looks.

     *

     *  Inputs:

     *      str = the input String

     *

     *  Some examples:

     *      xyzMiddle<- "AAxyzBB" -> true

     *      xyzMiddle<- "AxyzBB" -> true

     *      xyzMiddle<- "AxyzBBB" -> false

     */

    public static void main(String[] args)

    {

        Scanner cin = new Scanner(System.in);

        System.out.print("The input String: ");

        String str = cin.nextLine();


        boolean result = false;

        // PUT YOUR CODE ONLY BELOW THIS LINE

       

        int len = str.length();

        int i = 0;

        while (i <= len)

        {

            i++;

        }



        // PUT YOUR CODE ONLY ABOVE THIS LINE

        System.out.println(result);  // DO NOT MODIFY

    }

}


please help

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
What do you have so far? You have to check
2) Does xyz appear continuously in str?
1) Do characters left and right of xyz appear longer than 1 of eachother?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
heartybowl

heartybowl

    Newbie

  • Members
  • PipPip
  • 17 posts
nevermind I got it...
here is what I put

int len = str.length();
int i = 0;
while (i <= len - 3)
{
String a = str.substring(i, i + 3);
if (a.equals("xyz"))
{
String b = str.substring(0, i);
String c = str.substring(i + 3, len);
int len2 = b.length();
int len3 = c.length();
if ((len2 == len3) || (Math.abs(len2 - len3) == 1))
{
result = true;
}
}
i++;
}




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users