SlideShare a Scribd company logo
1 of 3
Please use Java to code the program, thank you!
Write a program that asks the user to enter a sentence. The program then passes the String into a
method called convertString that changes the String to an array of chars and returns the array.
The program then passes the array into a method called isReverse and returns true/false if the
array of chars is a palindrome. Ignore whitespace and punctuation (space, tab, single quote,
double quote, exclamation, comma, question mark, hyphen, semi-colon, colon, period). Ignore
case as well. For example, all these inputs are palindromes: A man, a plan, a canal, panama. Was
it a car or a cat I saw?"If I had a hi-fi!!" Use the following template: import java.util.Scanner;
Solution
Given below is the code for the question with output. Hope it helps. If it did, please do rate the
answer . Thank you very much
import java.util.Scanner;
public class Ex1 {
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String sentence = keybd.nextLine();
char[] arr = convertString(sentence);
if(isReverse(arr))
System.out.println(sentence + " is a palindrome");
else
System.out.println(sentence + " is NOT a palindrome");
}
//convert the input string into lower case and returns the array
public static char[] convertString(String s)
{
char[] arr = new char[s.length()];
for(int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if(ch >= 'a' && ch <= 'z') //lower case .. no change
arr[i] = ch;
else if(ch >= 'A' && ch <= 'Z') //is it upper case ? change to lower
arr[i] = (char) (ch - 'A' + 'a');
else //other characters no change
arr[i] = ch;
}
return arr;
}
//input is array of lower case letters and other special characters
public static boolean isReverse(char[] arr)
{
int i = 0;
int len = arr.length;
int j = arr.length - 1;
char ch1, ch2;
while(i < len && j >= 0)
{
//skip from left to right any non alphbetci characters, get the 1st letter from left
//to be matched.
do
{
ch1 = arr[i++];
}while(i < len && (ch1 < 'a' || ch1 > 'z'));
//skip from right to left any non alphabetic characters, get the 1st letter from left
//to be matched.s
do
{
ch2 = arr[j--];
}while(j >= 0 && (ch2 < 'a' || ch2 > 'z'));
if(ch1 != ch2)
return false;
}
return true;
}
}
output
Enter a sentence:
was it a car
was it a car is NOT a palindrome
Enter a sentence:
Was it a car OR A CAT I SAW??
Was it a car OR A CAT I SAW?? is a palindrome

More Related Content

Similar to Please use Java to code the program- thank you! Write a program that a.docx

The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
zone
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
birbal
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9
Vince Vo
 

Similar to Please use Java to code the program- thank you! Write a program that a.docx (20)

The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
Javascript
JavascriptJavascript
Javascript
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
 
3 character strings and formatted input output
3  character strings and formatted input output3  character strings and formatted input output
3 character strings and formatted input output
 
14 strings
14 strings14 strings
14 strings
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Javascript
JavascriptJavascript
Javascript
 
String in programming language in c or c++
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Cbasic
CbasicCbasic
Cbasic
 
Cbasic
CbasicCbasic
Cbasic
 
C programming
C programmingC programming
C programming
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
 
Strings
StringsStrings
Strings
 

More from rtodd884

Poly-meta-morphic malware looks different each time it is stored on di.docx
Poly-meta-morphic malware looks different each time it is stored on di.docxPoly-meta-morphic malware looks different each time it is stored on di.docx
Poly-meta-morphic malware looks different each time it is stored on di.docx
rtodd884
 
Please write a formal summary of the case before proceeding to answer.docx
Please write a formal summary of the case before proceeding to answer.docxPlease write a formal summary of the case before proceeding to answer.docx
Please write a formal summary of the case before proceeding to answer.docx
rtodd884
 
Please respond in detail with an example- Thank you! Financial interme.docx
Please respond in detail with an example- Thank you! Financial interme.docxPlease respond in detail with an example- Thank you! Financial interme.docx
Please respond in detail with an example- Thank you! Financial interme.docx
rtodd884
 
Problem 1- Text Editor using files and strings using C++ In this proje.docx
Problem 1- Text Editor using files and strings using C++ In this proje.docxProblem 1- Text Editor using files and strings using C++ In this proje.docx
Problem 1- Text Editor using files and strings using C++ In this proje.docx
rtodd884
 
Presented below is the balance sheet of Sargent Corporation for the cu.docx
Presented below is the balance sheet of Sargent Corporation for the cu.docxPresented below is the balance sheet of Sargent Corporation for the cu.docx
Presented below is the balance sheet of Sargent Corporation for the cu.docx
rtodd884
 
Prepare a short --talking points-- paper in which you identify and dis.docx
Prepare a short --talking points-- paper in which you identify and dis.docxPrepare a short --talking points-- paper in which you identify and dis.docx
Prepare a short --talking points-- paper in which you identify and dis.docx
rtodd884
 

More from rtodd884 (8)

Poly-meta-morphic malware looks different each time it is stored on di.docx
Poly-meta-morphic malware looks different each time it is stored on di.docxPoly-meta-morphic malware looks different each time it is stored on di.docx
Poly-meta-morphic malware looks different each time it is stored on di.docx
 
Please write a formal summary of the case before proceeding to answer.docx
Please write a formal summary of the case before proceeding to answer.docxPlease write a formal summary of the case before proceeding to answer.docx
Please write a formal summary of the case before proceeding to answer.docx
 
Please respond in detail with an example- Thank you! Financial interme.docx
Please respond in detail with an example- Thank you! Financial interme.docxPlease respond in detail with an example- Thank you! Financial interme.docx
Please respond in detail with an example- Thank you! Financial interme.docx
 
Problem 1- Text Editor using files and strings using C++ In this proje.docx
Problem 1- Text Editor using files and strings using C++ In this proje.docxProblem 1- Text Editor using files and strings using C++ In this proje.docx
Problem 1- Text Editor using files and strings using C++ In this proje.docx
 
Presented below is the balance sheet of Sargent Corporation for the cu.docx
Presented below is the balance sheet of Sargent Corporation for the cu.docxPresented below is the balance sheet of Sargent Corporation for the cu.docx
Presented below is the balance sheet of Sargent Corporation for the cu.docx
 
Presented below is information related to Dino Radja Company- Date End.docx
Presented below is information related to Dino Radja Company- Date End.docxPresented below is information related to Dino Radja Company- Date End.docx
Presented below is information related to Dino Radja Company- Date End.docx
 
Prepare the journal entry to record salaries payable for the month of.docx
Prepare the journal entry to record salaries payable for the month of.docxPrepare the journal entry to record salaries payable for the month of.docx
Prepare the journal entry to record salaries payable for the month of.docx
 
Prepare a short --talking points-- paper in which you identify and dis.docx
Prepare a short --talking points-- paper in which you identify and dis.docxPrepare a short --talking points-- paper in which you identify and dis.docx
Prepare a short --talking points-- paper in which you identify and dis.docx
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 

Recently uploaded (20)

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

Please use Java to code the program- thank you! Write a program that a.docx

  • 1. Please use Java to code the program, thank you! Write a program that asks the user to enter a sentence. The program then passes the String into a method called convertString that changes the String to an array of chars and returns the array. The program then passes the array into a method called isReverse and returns true/false if the array of chars is a palindrome. Ignore whitespace and punctuation (space, tab, single quote, double quote, exclamation, comma, question mark, hyphen, semi-colon, colon, period). Ignore case as well. For example, all these inputs are palindromes: A man, a plan, a canal, panama. Was it a car or a cat I saw?"If I had a hi-fi!!" Use the following template: import java.util.Scanner; Solution Given below is the code for the question with output. Hope it helps. If it did, please do rate the answer . Thank you very much import java.util.Scanner; public class Ex1 { public static void main(String[] args) { Scanner keybd = new Scanner(System.in); System.out.println("Enter a sentence: "); String sentence = keybd.nextLine(); char[] arr = convertString(sentence); if(isReverse(arr)) System.out.println(sentence + " is a palindrome"); else System.out.println(sentence + " is NOT a palindrome"); } //convert the input string into lower case and returns the array public static char[] convertString(String s) { char[] arr = new char[s.length()]; for(int i = 0; i < s.length(); i++) { char ch = s.charAt(i);
  • 2. if(ch >= 'a' && ch <= 'z') //lower case .. no change arr[i] = ch; else if(ch >= 'A' && ch <= 'Z') //is it upper case ? change to lower arr[i] = (char) (ch - 'A' + 'a'); else //other characters no change arr[i] = ch; } return arr; } //input is array of lower case letters and other special characters public static boolean isReverse(char[] arr) { int i = 0; int len = arr.length; int j = arr.length - 1; char ch1, ch2; while(i < len && j >= 0) { //skip from left to right any non alphbetci characters, get the 1st letter from left //to be matched. do { ch1 = arr[i++]; }while(i < len && (ch1 < 'a' || ch1 > 'z')); //skip from right to left any non alphabetic characters, get the 1st letter from left //to be matched.s do { ch2 = arr[j--]; }while(j >= 0 && (ch2 < 'a' || ch2 > 'z')); if(ch1 != ch2) return false; } return true; } } output Enter a sentence: was it a car was it a car is NOT a palindrome Enter a sentence:
  • 3. Was it a car OR A CAT I SAW?? Was it a car OR A CAT I SAW?? is a palindrome