簡體   English   中英

Java-將字符串的第一個字母移到末尾,並確定向后拼寫時單詞是否相同

[英]Java - moving first letter of string to the end, and determining if word is the same when spelled backwards

我正在嘗試編寫一個程序,當用戶使用掃描儀輸入字符串時,第一個字母移到單詞的末尾,然后單詞反拼。 然后,程序將確定您是否得到了原始單詞。

例如,如果用戶鍵入“ potato”,則程序將“ 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();

    }

}

這里是。

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();

}

這有效:

   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");

     }
}

通過一些觀察,這非常簡單。 您的問題是,您必須將第一個字符串移到最后,並檢查新字符串是否相同。

我的觀察:

對於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), 不需要額外的記憶體和空間,

我試圖編碼。 查看它是否有助於導入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");
   }
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM