2

I'm trying to make a program that when a user inputs a string using scanner, the first letter gets moved to the end of the word, and then the word is spelled backwards. The program then determines if you get the original word.

e.g if user types in 'potato' the program will move 'p' to the end, and will display true, as we get the same word backwards - 'otatop'.

Example output: You have entered "BANANA". Is ANANAB same as BANANA? True.

Thank you in advance for any help.

Jack

This is what I've got so far, but I don't think it works properly.

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

    }

}
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
zetbo
  • 78
  • 1
  • 6

4 Answers4

4

Here it is.

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

}
Rehan Javed
  • 418
  • 3
  • 14
1

This works:

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

     }
}
George Cernat
  • 1,323
  • 9
  • 27
1

This is very simple with some of observation. Your question is you have to move the first latter to the end and check reverse if the new string is same or not.

My ovservation:

For BANANA new string is ANANAB. Now reverse the string and check weather it is same as the first one. Now If you ignore the first char B the string will be ANANA. As you have to reverse the string and check this one is same as the first one so this is like palindrome problem. For the input BANANA ANANA is palindrome. We are moving the first char to the end so there is no impact of it on checking palindrome. So I ignore the first char and check the rest is palindrome or not.

The Method is like:

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;
}

The main function is:

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

    }

The Runtime for this problem is n/2 means O(n) and no extra memory and space needed,

1

I have tried to code it. See if it helps 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");
   }
 }
}
Namrata Shukla
  • 157
  • 2
  • 8