SlideShare a Scribd company logo
1 of 4
Download to read offline
/*
* The java program that prompts user to
* enter a string and shift key and then
* converts the string to the encoded string
* and prints the encoded string value to console.
* */
//CeaserCipher.java
import java.util.Scanner;
public class CeaserCipher
{
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
String message;
int shift;
System.out.println("Enter a message in small letters,a-z");
message=scanner.nextLine();
System.out.println("Enter shift value");
shift=scanner.nextInt();
//calling encode method with message, shift value
String encodedMsg=encode(message,shift);
//calling WriteMessage
WriteMessage(encodedMsg);
}
/*Method that prints the encoded text to console*/
private static void WriteMessage(String encodedMsg)
{
System.out.println("Encoded text :"+encodedMsg);
}
//Method that takes string and integer value and returns
//the encoded string to calling method
private static String encode(String message, int shift)
{
StringBuffer sb=new StringBuffer(message);
for (int i = 0; i < message.length(); i++)
{
//shift to back to the alphabets
char encodedkey = (char) ('a' + (message.charAt(i) -'a' + shift) % ('z' - 'a' + 1));
//set character at index,i
sb.setCharAt(i, encodedkey);
}
//return encoded string to calling method
return new String(sb);
}
}
Sample Output:
Enter a message in small letters,a-z
sunday is holiday
Enter shift value
5
Encoded text :xzsifdYnxYmtqnifd
Solution
/*
* The java program that prompts user to
* enter a string and shift key and then
* converts the string to the encoded string
* and prints the encoded string value to console.
* */
//CeaserCipher.java
import java.util.Scanner;
public class CeaserCipher
{
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
String message;
int shift;
System.out.println("Enter a message in small letters,a-z");
message=scanner.nextLine();
System.out.println("Enter shift value");
shift=scanner.nextInt();
//calling encode method with message, shift value
String encodedMsg=encode(message,shift);
//calling WriteMessage
WriteMessage(encodedMsg);
}
/*Method that prints the encoded text to console*/
private static void WriteMessage(String encodedMsg)
{
System.out.println("Encoded text :"+encodedMsg);
}
//Method that takes string and integer value and returns
//the encoded string to calling method
private static String encode(String message, int shift)
{
StringBuffer sb=new StringBuffer(message);
for (int i = 0; i < message.length(); i++)
{
//shift to back to the alphabets
char encodedkey = (char) ('a' + (message.charAt(i) -'a' + shift) % ('z' - 'a' + 1));
//set character at index,i
sb.setCharAt(i, encodedkey);
}
//return encoded string to calling method
return new String(sb);
}
}
Sample Output:
Enter a message in small letters,a-z
sunday is holiday
Enter shift value
5
Encoded text :xzsifdYnxYmtqnifd

More Related Content

Similar to The java program that prompts user to enter a string and .pdf

Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptxKimVeeL
 
the code below is a recommendation system application to suggest a s.pdf
the code below is a recommendation system application to suggest a s.pdfthe code below is a recommendation system application to suggest a s.pdf
the code below is a recommendation system application to suggest a s.pdfrajatchugh13
 
Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptxKimVeeL
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docxKatecate1
 
In JavaIn a 2D Arraya.)Display the row and column with the hig.pdf
In JavaIn a 2D Arraya.)Display the row and column with the hig.pdfIn JavaIn a 2D Arraya.)Display the row and column with the hig.pdf
In JavaIn a 2D Arraya.)Display the row and column with the hig.pdffeelingspaldi
 
I am working on java programming that converts zipcode to barcode an.pdf
I am working on java programming that converts zipcode to barcode an.pdfI am working on java programming that converts zipcode to barcode an.pdf
I am working on java programming that converts zipcode to barcode an.pdfthangarajarivukadal
 
I need an explaining for each step in this code and the reason of it-.docx
I need an explaining for each step in this code and the reason of it-.docxI need an explaining for each step in this code and the reason of it-.docx
I need an explaining for each step in this code and the reason of it-.docxhendriciraida
 
network programing lab file ,
network programing lab file ,network programing lab file ,
network programing lab file ,AAlha PaiKra
 
แก้งานย่อย 7
แก้งานย่อย 7แก้งานย่อย 7
แก้งานย่อย 7Itslvle Parin
 
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfMagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfanjanacottonmills
 

Similar to The java program that prompts user to enter a string and .pdf (15)

Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
the code below is a recommendation system application to suggest a s.pdf
the code below is a recommendation system application to suggest a s.pdfthe code below is a recommendation system application to suggest a s.pdf
the code below is a recommendation system application to suggest a s.pdf
 
Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptx
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docx
 
Vo.pdf
   Vo.pdf   Vo.pdf
Vo.pdf
 
In JavaIn a 2D Arraya.)Display the row and column with the hig.pdf
In JavaIn a 2D Arraya.)Display the row and column with the hig.pdfIn JavaIn a 2D Arraya.)Display the row and column with the hig.pdf
In JavaIn a 2D Arraya.)Display the row and column with the hig.pdf
 
07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt
 
I am working on java programming that converts zipcode to barcode an.pdf
I am working on java programming that converts zipcode to barcode an.pdfI am working on java programming that converts zipcode to barcode an.pdf
I am working on java programming that converts zipcode to barcode an.pdf
 
PROGRAMMING QUESTIONS.docx
PROGRAMMING QUESTIONS.docxPROGRAMMING QUESTIONS.docx
PROGRAMMING QUESTIONS.docx
 
I need an explaining for each step in this code and the reason of it-.docx
I need an explaining for each step in this code and the reason of it-.docxI need an explaining for each step in this code and the reason of it-.docx
I need an explaining for each step in this code and the reason of it-.docx
 
network programing lab file ,
network programing lab file ,network programing lab file ,
network programing lab file ,
 
3.Lesson Plan - Input.pdf.pdf
3.Lesson Plan - Input.pdf.pdf3.Lesson Plan - Input.pdf.pdf
3.Lesson Plan - Input.pdf.pdf
 
แก้งานย่อย 7
แก้งานย่อย 7แก้งานย่อย 7
แก้งานย่อย 7
 
Java doc Pr ITM2
Java doc Pr ITM2Java doc Pr ITM2
Java doc Pr ITM2
 
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfMagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
 

More from DEEPAKSONI562

S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf
 S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf
S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdfDEEPAKSONI562
 
The answer is b, theres no increase in H. .pdf
                     The answer is b, theres no increase in H.        .pdf                     The answer is b, theres no increase in H.        .pdf
The answer is b, theres no increase in H. .pdfDEEPAKSONI562
 
Step1 white ppt with NaCl(aq) indicates Ag+ Step.pdf
                     Step1 white ppt with NaCl(aq) indicates Ag+  Step.pdf                     Step1 white ppt with NaCl(aq) indicates Ag+  Step.pdf
Step1 white ppt with NaCl(aq) indicates Ag+ Step.pdfDEEPAKSONI562
 
Natural rubber is a polymer of isoprene units ; e.pdf
                     Natural rubber is a polymer of isoprene units ; e.pdf                     Natural rubber is a polymer of isoprene units ; e.pdf
Natural rubber is a polymer of isoprene units ; e.pdfDEEPAKSONI562
 
no double displacement reactions can be mixing tw.pdf
                     no double displacement reactions can be mixing tw.pdf                     no double displacement reactions can be mixing tw.pdf
no double displacement reactions can be mixing tw.pdfDEEPAKSONI562
 
using System; using System.Collections.Generic; using System.Lin.pdf
using System; using System.Collections.Generic; using System.Lin.pdfusing System; using System.Collections.Generic; using System.Lin.pdf
using System; using System.Collections.Generic; using System.Lin.pdfDEEPAKSONI562
 
Toxicants may move across biological membranes by all the following .pdf
Toxicants may move across biological membranes by all the following .pdfToxicants may move across biological membranes by all the following .pdf
Toxicants may move across biological membranes by all the following .pdfDEEPAKSONI562
 
time is 18.2yearsdetailed answer i will submit nowSolutionti.pdf
time is 18.2yearsdetailed answer i will submit nowSolutionti.pdftime is 18.2yearsdetailed answer i will submit nowSolutionti.pdf
time is 18.2yearsdetailed answer i will submit nowSolutionti.pdfDEEPAKSONI562
 
This is the code for the above 5 public class Input extends JFram.pdf
This is the code for the above 5 public class Input extends JFram.pdfThis is the code for the above 5 public class Input extends JFram.pdf
This is the code for the above 5 public class Input extends JFram.pdfDEEPAKSONI562
 
Team public class Team {    private String teamId;    priva.pdf
Team public class Team {    private String teamId;    priva.pdfTeam public class Team {    private String teamId;    priva.pdf
Team public class Team {    private String teamId;    priva.pdfDEEPAKSONI562
 
PollinationFertilization1.     Pollination is the transfer of po.pdf
PollinationFertilization1.     Pollination is the transfer of po.pdfPollinationFertilization1.     Pollination is the transfer of po.pdf
PollinationFertilization1.     Pollination is the transfer of po.pdfDEEPAKSONI562
 
Patient.java package A9.toStudents; public class Patient imple.pdf
Patient.java package A9.toStudents; public class Patient imple.pdfPatient.java package A9.toStudents; public class Patient imple.pdf
Patient.java package A9.toStudents; public class Patient imple.pdfDEEPAKSONI562
 
Non financial performance indicators of United Utilities Group PLC. .pdf
Non financial performance indicators of United Utilities Group PLC. .pdfNon financial performance indicators of United Utilities Group PLC. .pdf
Non financial performance indicators of United Utilities Group PLC. .pdfDEEPAKSONI562
 
C) III is correct note the configuration keep un.pdf
                     C) III is correct note the configuration keep un.pdf                     C) III is correct note the configuration keep un.pdf
C) III is correct note the configuration keep un.pdfDEEPAKSONI562
 
LHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdf
LHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdfLHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdf
LHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdfDEEPAKSONI562
 
import com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdf
import com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdfimport com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdf
import com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdfDEEPAKSONI562
 
intervalSolutioninterval.pdf
intervalSolutioninterval.pdfintervalSolutioninterval.pdf
intervalSolutioninterval.pdfDEEPAKSONI562
 
In2O3SolutionIn2O3.pdf
In2O3SolutionIn2O3.pdfIn2O3SolutionIn2O3.pdf
In2O3SolutionIn2O3.pdfDEEPAKSONI562
 
FeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdf
FeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdfFeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdf
FeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdfDEEPAKSONI562
 
D the polar covalent bond within methanol cannot form hydrogen bond .pdf
D the polar covalent bond within methanol cannot form hydrogen bond .pdfD the polar covalent bond within methanol cannot form hydrogen bond .pdf
D the polar covalent bond within methanol cannot form hydrogen bond .pdfDEEPAKSONI562
 

More from DEEPAKSONI562 (20)

S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf
 S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf
S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf
 
The answer is b, theres no increase in H. .pdf
                     The answer is b, theres no increase in H.        .pdf                     The answer is b, theres no increase in H.        .pdf
The answer is b, theres no increase in H. .pdf
 
Step1 white ppt with NaCl(aq) indicates Ag+ Step.pdf
                     Step1 white ppt with NaCl(aq) indicates Ag+  Step.pdf                     Step1 white ppt with NaCl(aq) indicates Ag+  Step.pdf
Step1 white ppt with NaCl(aq) indicates Ag+ Step.pdf
 
Natural rubber is a polymer of isoprene units ; e.pdf
                     Natural rubber is a polymer of isoprene units ; e.pdf                     Natural rubber is a polymer of isoprene units ; e.pdf
Natural rubber is a polymer of isoprene units ; e.pdf
 
no double displacement reactions can be mixing tw.pdf
                     no double displacement reactions can be mixing tw.pdf                     no double displacement reactions can be mixing tw.pdf
no double displacement reactions can be mixing tw.pdf
 
using System; using System.Collections.Generic; using System.Lin.pdf
using System; using System.Collections.Generic; using System.Lin.pdfusing System; using System.Collections.Generic; using System.Lin.pdf
using System; using System.Collections.Generic; using System.Lin.pdf
 
Toxicants may move across biological membranes by all the following .pdf
Toxicants may move across biological membranes by all the following .pdfToxicants may move across biological membranes by all the following .pdf
Toxicants may move across biological membranes by all the following .pdf
 
time is 18.2yearsdetailed answer i will submit nowSolutionti.pdf
time is 18.2yearsdetailed answer i will submit nowSolutionti.pdftime is 18.2yearsdetailed answer i will submit nowSolutionti.pdf
time is 18.2yearsdetailed answer i will submit nowSolutionti.pdf
 
This is the code for the above 5 public class Input extends JFram.pdf
This is the code for the above 5 public class Input extends JFram.pdfThis is the code for the above 5 public class Input extends JFram.pdf
This is the code for the above 5 public class Input extends JFram.pdf
 
Team public class Team {    private String teamId;    priva.pdf
Team public class Team {    private String teamId;    priva.pdfTeam public class Team {    private String teamId;    priva.pdf
Team public class Team {    private String teamId;    priva.pdf
 
PollinationFertilization1.     Pollination is the transfer of po.pdf
PollinationFertilization1.     Pollination is the transfer of po.pdfPollinationFertilization1.     Pollination is the transfer of po.pdf
PollinationFertilization1.     Pollination is the transfer of po.pdf
 
Patient.java package A9.toStudents; public class Patient imple.pdf
Patient.java package A9.toStudents; public class Patient imple.pdfPatient.java package A9.toStudents; public class Patient imple.pdf
Patient.java package A9.toStudents; public class Patient imple.pdf
 
Non financial performance indicators of United Utilities Group PLC. .pdf
Non financial performance indicators of United Utilities Group PLC. .pdfNon financial performance indicators of United Utilities Group PLC. .pdf
Non financial performance indicators of United Utilities Group PLC. .pdf
 
C) III is correct note the configuration keep un.pdf
                     C) III is correct note the configuration keep un.pdf                     C) III is correct note the configuration keep un.pdf
C) III is correct note the configuration keep un.pdf
 
LHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdf
LHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdfLHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdf
LHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdf
 
import com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdf
import com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdfimport com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdf
import com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdf
 
intervalSolutioninterval.pdf
intervalSolutioninterval.pdfintervalSolutioninterval.pdf
intervalSolutioninterval.pdf
 
In2O3SolutionIn2O3.pdf
In2O3SolutionIn2O3.pdfIn2O3SolutionIn2O3.pdf
In2O3SolutionIn2O3.pdf
 
FeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdf
FeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdfFeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdf
FeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdf
 
D the polar covalent bond within methanol cannot form hydrogen bond .pdf
D the polar covalent bond within methanol cannot form hydrogen bond .pdfD the polar covalent bond within methanol cannot form hydrogen bond .pdf
D the polar covalent bond within methanol cannot form hydrogen bond .pdf
 

Recently uploaded

OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 

Recently uploaded (20)

OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 

The java program that prompts user to enter a string and .pdf

  • 1. /* * The java program that prompts user to * enter a string and shift key and then * converts the string to the encoded string * and prints the encoded string value to console. * */ //CeaserCipher.java import java.util.Scanner; public class CeaserCipher { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String message; int shift; System.out.println("Enter a message in small letters,a-z"); message=scanner.nextLine(); System.out.println("Enter shift value"); shift=scanner.nextInt(); //calling encode method with message, shift value String encodedMsg=encode(message,shift); //calling WriteMessage WriteMessage(encodedMsg); } /*Method that prints the encoded text to console*/ private static void WriteMessage(String encodedMsg) { System.out.println("Encoded text :"+encodedMsg); } //Method that takes string and integer value and returns //the encoded string to calling method
  • 2. private static String encode(String message, int shift) { StringBuffer sb=new StringBuffer(message); for (int i = 0; i < message.length(); i++) { //shift to back to the alphabets char encodedkey = (char) ('a' + (message.charAt(i) -'a' + shift) % ('z' - 'a' + 1)); //set character at index,i sb.setCharAt(i, encodedkey); } //return encoded string to calling method return new String(sb); } } Sample Output: Enter a message in small letters,a-z sunday is holiday Enter shift value 5 Encoded text :xzsifdYnxYmtqnifd Solution /* * The java program that prompts user to * enter a string and shift key and then * converts the string to the encoded string * and prints the encoded string value to console. * */ //CeaserCipher.java import java.util.Scanner; public class CeaserCipher { public static void main(String[] args)
  • 3. { Scanner scanner=new Scanner(System.in); String message; int shift; System.out.println("Enter a message in small letters,a-z"); message=scanner.nextLine(); System.out.println("Enter shift value"); shift=scanner.nextInt(); //calling encode method with message, shift value String encodedMsg=encode(message,shift); //calling WriteMessage WriteMessage(encodedMsg); } /*Method that prints the encoded text to console*/ private static void WriteMessage(String encodedMsg) { System.out.println("Encoded text :"+encodedMsg); } //Method that takes string and integer value and returns //the encoded string to calling method private static String encode(String message, int shift) { StringBuffer sb=new StringBuffer(message); for (int i = 0; i < message.length(); i++) { //shift to back to the alphabets char encodedkey = (char) ('a' + (message.charAt(i) -'a' + shift) % ('z' - 'a' + 1)); //set character at index,i sb.setCharAt(i, encodedkey); } //return encoded string to calling method
  • 4. return new String(sb); } } Sample Output: Enter a message in small letters,a-z sunday is holiday Enter shift value 5 Encoded text :xzsifdYnxYmtqnifd