Java - 将字符串的第一个字母移到最后,并在向后拼写时确定单词是否相同

时间:2017-11-16 14:40:33

标签: java

我正在尝试制作一个程序,当用户使用扫描仪输入字符串时,第一个字母会移到单词的末尾,然后单词拼写向后。程序然后确定你是否得到原始单词。

例如,如果用户输入'马铃薯',程序会将'p'移动到最后,并显示为true,因为我们向后移动相同的单词 - 'otatop'。

示例输出: 您已输入“BANANA”。 ANANAB和BANANA一样吗?真。

提前感谢您的帮助。

杰克

这是我到目前为止所得到的,但我认为它不能正常运作。

public class WordPlay {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String word;
        String palindrome = "";
        String quit = "quit";


        do {
            System.out.print("Enter a word: ");
            word = scanner.nextLine().toUpperCase();

            int length = word.length();

            for (int i = length - 1; i >= 0; i--) {
                palindrome = palindrome + word.charAt(i);
            }

            if (word.equals(palindrome)) {
                System.out.println("Is the word + palindrome + " same as " + word + "?", true);
            } else {
                System.out.println(false);
            }
        } while (!word.equals(quit));
        System.out.println("Good Bye");     
        scanner.close();

    }

}

4 个答案:

答案 0 :(得分:1)

在这里。

public static void main(String[] args) {

    // To take input.
    Scanner scan = new Scanner(System.in);

    System.out.print("Enter Word: ");
    String word = scan.next(); // taking the word from user

    // moving first letter to the end.
    String newWord = word.substring(1) + word.charAt(0);

    // reversing the newWord.
    String reversed = new StringBuffer(newWord).reverse().toString();

    // printing output.
    System.out.println("You have entered '"+word+"'. "
            + "Is "+newWord+" same as "+word+"? "
            +reversed.equals(word)+".");

    // closing the input stream.
    scan.close();

}

答案 1 :(得分:1)

这有效:

   import java.util.*;

public class HelloWorld{

     public static void main(String []args){
         Scanner scan = new Scanner(System.in);
        String s1 = scan.next();
        char s2 = s1.charAt(0);
        String s3 = s1.substring(1) + s2;
        s3 = new StringBuilder(s3).reverse().toString();
        if(s1.equals(s3))
            System.out.println("They are same");
        else
            System.out.println("They are not the same");

     }
}

答案 2 :(得分:1)

通过一些观察,这非常简单。你的问题是你必须将第一个后者移到最后,如果新字符串相同则检查反向。

我的卵子:

对于BANANA,新字符串为ANANAB。现在反转字符串并检查天气与第一个相同。 现在如果忽略第一个字符B,字符串将为ANANA。因为你必须反转字符串并检查它与第一个相同,所以这就像palindrome问题。对于输入BANANA ANANA是回文。我们正在将第一个字符移动到最后,因此它对检查回文没有任何影响。所以我忽略了第一个字符,并检查其余是否是回文。

方法如下:

private static boolean getAns(String word) {

        int st = 1;
        int en = word.length() - 1;

        while (st < en) {
            if (word.charAt(en) != word.charAt(st)) {
                return false;
            }
            st++;
            en--;
        }

        return true;
}

主要功能是:

public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("Input your String:");
        String word = scanner.nextLine();

        boolean ans = getAns(word);

        System.out.println("You have entered " + word + ". Is " + word.substring(1) + word.charAt(0) + " same as " + word + "? : " + ans + ".");

    }

此问题的运行时 n / 2 表示O(n)和无额外内存,需要空间,

答案 3 :(得分:1)

我试过编码。看看它是否有帮助     import java.util.Scanner;

class StringCheck
{
 public static void main(String[] args)
 {
   Scanner sc = new Scanner(System.in);
   String str = new String();
   String tempstr = new String();
   System.out.println("Enter your String ");
   str = sc.next();
   int len = str.length();
//putting first character of str at last of tempstr
for (int i = 1 ; i<len; i++)
{
  tempstr += str.charAt(i);
}
tempstr += str.charAt(0);
//reversing tempstr
char[] tempchar = tempstr.toCharArray();
int j = len-1;
char temp;
for ( int i = 0; i<len/2 ; i++)
{
  if(i<j)
  {
    temp = tempchar[i];
    tempchar[i] = tempchar[j];
    tempchar[j]= temp;
    j--;
  }
  else
  break;
 }
 //reversing completed
 tempstr = new String(tempchar);
 //  System.out.println("the reversed string is "+tempstr);
    if(str.compareTo(tempstr)==0)
   {
     System.out.println("true");
   }
    else
   {
     System.out.println("false");
   }
 }
}