Skip to main content
typo
Source Link
jacwah
  • 2.7k
  • 18
  • 42

Your implementedYou're implementation can be improved which inturnsin turn can save some memory space and execution time. Space is saved by not using another string in stringReverse and time is saved by iterating from 0 - (strlen()-1)/2 instead of 0 - strlen().

Hope this helps!

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Palindrome {
public static void main(String[] args) throws IOException {

    // user enters string
    System.out.println ("Please enter string");

    InputStreamReader input = new InputStreamReader (System.in);
    BufferedReader reader = new BufferedReader (input);
    String userinput = "";
    userinput = reader.readLine();

    // test if string is valid
        if (!userinput.matches("[A-Za-z]+")){
            System.out.println ("Invalid Input");
            System.exit(1);
        }

        else if (userinput.length() > 10){
            System.out.println ("String too long");
            System.exit(2);
        }

       //Modified this part
       int n = userinput.length();
       for (int i = 0; i <= (n - 1) / 2; i++){
         if(userinput.charAt(i) != userinput.charAt(n-1-i)) {
            System.out.println("String is not a palindrome");
            System.exit(0);
         }
       }

       System.out.println("String is a palindrome");

    }
}

Your implemented can be improved which inturns can save some memory space and execution time. Space is saved by not using another string stringReverse and time is saved by iterating from 0 - (strlen()-1)/2 instead of 0 - strlen().

Hope this helps!

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Palindrome {
public static void main(String[] args) throws IOException {

    // user enters string
    System.out.println ("Please enter string");

    InputStreamReader input = new InputStreamReader (System.in);
    BufferedReader reader = new BufferedReader (input);
    String userinput = "";
    userinput = reader.readLine();

    // test if string is valid
        if (!userinput.matches("[A-Za-z]+")){
            System.out.println ("Invalid Input");
            System.exit(1);
        }

        else if (userinput.length() > 10){
            System.out.println ("String too long");
            System.exit(2);
        }

       //Modified this part
       int n = userinput.length();
       for (int i = 0; i <= (n - 1) / 2; i++){
         if(userinput.charAt(i) != userinput.charAt(n-1-i)) {
            System.out.println("String is not a palindrome");
            System.exit(0);
         }
       }

       System.out.println("String is a palindrome");

    }
}

You're implementation can be improved which in turn can save some memory space and execution time. Space is saved by not using another string in stringReverse and time is saved by iterating from 0 - (strlen()-1)/2 instead of 0 - strlen().

Hope this helps!

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Palindrome {
public static void main(String[] args) throws IOException {

    // user enters string
    System.out.println ("Please enter string");

    InputStreamReader input = new InputStreamReader (System.in);
    BufferedReader reader = new BufferedReader (input);
    String userinput = "";
    userinput = reader.readLine();

    // test if string is valid
        if (!userinput.matches("[A-Za-z]+")){
            System.out.println ("Invalid Input");
            System.exit(1);
        }

        else if (userinput.length() > 10){
            System.out.println ("String too long");
            System.exit(2);
        }

       //Modified this part
       int n = userinput.length();
       for (int i = 0; i <= (n - 1) / 2; i++){
         if(userinput.charAt(i) != userinput.charAt(n-1-i)) {
            System.out.println("String is not a palindrome");
            System.exit(0);
         }
       }

       System.out.println("String is a palindrome");

    }
}
Source Link
eshb
  • 11
  • 3

Your implemented can be improved which inturns can save some memory space and execution time. Space is saved by not using another string stringReverse and time is saved by iterating from 0 - (strlen()-1)/2 instead of 0 - strlen().

Hope this helps!

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Palindrome {
public static void main(String[] args) throws IOException {

    // user enters string
    System.out.println ("Please enter string");

    InputStreamReader input = new InputStreamReader (System.in);
    BufferedReader reader = new BufferedReader (input);
    String userinput = "";
    userinput = reader.readLine();

    // test if string is valid
        if (!userinput.matches("[A-Za-z]+")){
            System.out.println ("Invalid Input");
            System.exit(1);
        }

        else if (userinput.length() > 10){
            System.out.println ("String too long");
            System.exit(2);
        }

       //Modified this part
       int n = userinput.length();
       for (int i = 0; i <= (n - 1) / 2; i++){
         if(userinput.charAt(i) != userinput.charAt(n-1-i)) {
            System.out.println("String is not a palindrome");
            System.exit(0);
         }
       }

       System.out.println("String is a palindrome");

    }
}