-4

how to check if the first half of a string is the same as the second half of the string? (not to be confused with palindromes) This is my code so far.

   Scanner oIn = new Scanner (System.in);
   String sFB = oIn.nextLine();
   int length = sFB.length();

   boolean bYes = true;
   char cA, cB;

   for (int i = 0; i < length/2; i++){
      cA = sFB.charAt(i);
      cB = sFB.charAt(/*what do i put here?*/);

       if (cA != cB){
           bYes = false;
           break;
       }
   }

   if (bYes) {
       System.out.println("It's the same");
   }
   else {
       System.out.println("It's not the same");
   }

Example String Input: abab

Output: It's the same

JavaFTW
  • 3
  • 3
  • 6
  • 1
    Consider providing an example of what you are trying to do. Maybe an example of a string you are testing? – v4570 Jan 18 '18 at 00:57
  • 2
    It's unclear what you're asking. Please [edit] your post to be more specific, with a sample of the string and a clearer explanation than *the same as*. While you're at it, include the code you've written in an effort to solve the problem yourself. – Ken White Jan 18 '18 at 01:00
  • You use `substring` and `equals`: `int len = (s.length() + 1) / 2; if (s.substring(0, len).equals(s.substring(s.length() - len))) { ... }` – Andreas Jan 18 '18 at 01:10
  • When You say String. It can be as String input="String"; or String input="I am a String".So Please provide your inputs and expected outputs. That will be helpful. – kemparaj565 Jan 18 '18 at 01:15
  • The second half of `abba` is not the same as the first half. – Andreas Jan 18 '18 at 01:17
  • The word that describes the test you’re trying to perform is [palindrome](https://en.wikipedia.org/wiki/Palindrome). Adding that word to your question (for instance, “How do I check if a String is a palindrome”) will make your question clearer. – VGR Jan 18 '18 at 01:18
  • @VGR, thank you. i knew his intent. –  Jan 18 '18 at 01:19
  • sometimes when u r knew to programming you have a hard time expressing your intent. –  Jan 18 '18 at 01:19
  • 1
    Possible duplicate of [Check string for palindrome](https://stackoverflow.com/questions/4138827/check-string-for-palindrome) – Javier Jan 18 '18 at 01:20
  • Actually, I'm not checking for a palindrome, that would be a mirror ("abba") but I trying to check if it's the same on both halves ("abab") – JavaFTW Jan 18 '18 at 01:20
  • LOL. I will change it. –  Jan 18 '18 at 01:20
  • Then you can use Andreas’s code, or just `sFB.endsWith(sFB.substring(0, sFB.length() / 2))`. – VGR Jan 18 '18 at 01:23

3 Answers3

2

If your input is of length of odd number then its not same as second half.

if(sFB.length()%2!=0){
System.out.println("It's not the same");
}

Complete code as below.

public class StringEqualsSecondHalf {

public static void main(String[] args) {
    String sFB="String";
    if(sFB.length()%2!=0){
        System.out.println("Its not same");
    }
    else{
        String firstHalf=sFB.substring(0,sFB.length()/2);
        System.out.println("First Half "+firstHalf);
        String secondHalf=sFB.substring(sFB.length()/2,sFB.length());
        System.out.println("Second Half "+secondHalf);
        if(firstHalf.equals(secondHalf)){
            System.out.println("They are same");
        }
        else{
            System.out.println("They are not same");
        }
    }

}

}

kemparaj565
  • 379
  • 3
  • 6
0

You have two case: if the length of string is odd or even.

To check if a String s is even or odd, you % it's length.

To solve your algorithm replace it with this:

 cB = sFB.charAt(i + length / 2);

You can also use substring in the String class.

You can use substring to get the first half and to get the second half.

public static boolean sameAsHalf(String s){

    if(s.length() == 1){
        return true;
    }

    if(s.length() % 2 == 0){

        String firstHalf = s.substring(0, s.length()/2);
        System.out.println(firstHalf);

        String secondHalf = s.substring(s.length()/2);
        System.out.println(secondHalf);
        return firstHalf.equals(secondHalf);
    }

    return false;
}

If you are coming from other languages, use equals to check if the content of the strings are equal.

  • That checks if the second half is a *mirror* of the first half, e.g. `abccba`. Not what question was asking for, e.g. `abcabc` (first half same as second half). – Andreas Jan 18 '18 at 01:12
  • If you think question is unclear, then you ask for clarification before answering. I'm going by what is actually written in the question: *first half **same as** second half*. "Same as", not backwards or mirrored. – Andreas Jan 18 '18 at 01:15
  • your `else` could just return `false`, since a string of odd length will never be equally split. – RustyPiranha Jan 18 '18 at 01:36
0
public static boolean halfEquals(String string) {

    char[] stringArray = string.toCharArray();

    int firstHalfEnd = (stringArray.length / 2);
    int seconHalfStart = (stringArray.length / 2) + (stringArray.length % 2);

    for (int position = 0; position <  firstHalfEnd; position++) {
        if (stringArray[position] != stringArray[position + seconHalfStart]) {
            return false;
        }
    }

    return true;
}

String.toCharArray() is the key, way simpler than charAt().

Edit output:

abab ->true

abcab ->true

abde ->false

abcde ->false

x ->true

->true

ronchi82
  • 138
  • 1
  • 5