2011-10-12 1 views
0

문자열이 회문인지 여부를 결정할 때 공백, 구두점 및 대문자와 소문자를 고려하지 않은 Palindrome 프로그램을 만들려고합니다.공백, 구두점 및 대문자를 고려하지 않은 Palindrome 프로그램

앞서 언급 한 코드를 어떻게 변경할 수 있습니까? 문자가 같은 읽기 왼쪽에서 오른쪽 및 오른쪽에서 왼쪽으로 (즉 결국이며, 어떤 경우 현재 코드가 변수 str 및 검사에 보이는

+2

이 숙제가 있습니까? 그렇다면 [tag : Homework] 태그를 추가하십시오. 또한, 무엇을 시도 했습니까? 아니면 현재 코드가하고 싶은 일을하고 있습니까? – brc

+1

유휴 호기심 : 숙제에 관한 히스토그램을 그렸습니다. 날짜와 대다수의 교과 과정에서 특정 유형의 운동이 나오기 쉬운지를 묻는 날짜가 있습니까? 강한 추세가있을 것입니다 ;-) –

답변

0

package palindrome; 

import java.util.Scanner; 

public class Palindrome { 

    public static void main (String[] args) 
{ 
    String str, another = "y"; 
    int left, right; 
    Scanner scan = new Scanner (System.in); 

    while (another.equalsIgnoreCase("y")) // allows y or Y 
    { 
    System.out.println ("Enter a potential palindrome:"); 
    str = scan.nextLine(); 



    left = 0; 
    right = str.length() - 1; 

    while (str.charAt(left) == str.charAt(right) && left < right) 
    { 
     left++; 
     right--; 
    } 

    System.out.println(); 

    if (left < right) 
     System.out.println ("That string is NOT a palindrome."); 
    else 
     System.out.println ("That string IS a palindrome."); 

    System.out.println(); 
    System.out.print ("Test another palindrome (y/n)? "); 
    another = scan.nextLine(); 
    } 

} } palindrome is).

현재 사용자가 입력 한 원래 문자열에 해당 내용이 있습니다. 내 측 while -loop 전에 변경하려면 str 변수를 필터링하십시오. 무엇을 필터링할지 정확히 알아보기 위해 indexOf(), replace()substring()과 같은 String 클래스의 유용한 메소드를 살펴 보겠습니다.

0
The heart of your program is in following loop 

while (str.charAt(left) == str.charAt(right) && left < right) 
    { 
     left++; 
     right--; 
    } 


what you are doing here is to compare the characters without honoring the condition of ignoring space and punctuation here. Your logic should be such that while picking the characters for comparison (either from left or right) you will need to skip that character and move for the next character. 
To make picture clearer see the following : 

Input string : 
inStr = Ma'lyalam 

Step 1: 
Since you have to ignore the case do following 
inStr = inStr.toLowerCase(); 

Step 2: 

1. int left =0 , right = 8 
2. char chLeft, chRight 
3. chLeft = m , chRight = m 
4. chLeft == chRight -> true, increment left and decrement right 
5. chLeft = a , chRight = a 
6. chLeft == chRight -> true, increment left and decrement right 
7. chLeft = ' , chRight = l -> Since chLeft = ' skip it, increment left so chLeft = l. Now continue like above 

so the code should look like 

    boolean isPlaindrome = false; 
    str = str.toLowerCase(); 
    char chLeft, chRight; 
    while (left < right) 
    { 
    chLeft = str.charAt(left); 
    chRight = str.charAt(right) 
    if (chLeft == ''' || chLeft == ' ') 
    { 
     left++ 
     continue; 
    } 
    else if (chRight == ''' || chRight == ' ') 
    { 
     right--; 
     continue; 
    } 

    if (chLeft == chRight) 
    { 
    left++; right--; 
    } 
    else 
    { 
    break; 
    } 
    } 

    if (left == right) 
    isPlaindrome = true;