Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package litery;
- import java.awt.List;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import javax.swing.JOptionPane;
- public class Main {
- public static void main(String[] args) {
- String slowo, slowo2;
- int wybor=Integer.parseInt(JOptionPane.showInputDialog("Co chcesz zrobic?\n1.Sprawdzic czy slowo jest palindromem\n2.Sprawdzic czy slowa sa anagramami"));
- while(wybor!=1 && wybor!=2){
- if(wybor!=1 && wybor!=2) {
- wybor=Integer.parseInt(JOptionPane.showInputDialog("Wybrales niepoprawna opcje. \n1. Sprawdzic czy slowo jest palindromem\n2.Sprawdzic czy slowa sa anagramami"));}}
- switch(wybor){
- case 1: {
- slowo=JOptionPane.showInputDialog("Podaj slowo, ktore chcesz sprawdzic czy jest palindromem");
- slowo=slowo.toLowerCase();
- slowo2=new StringBuilder(slowo).reverse().toString();
- boolean czy=new String(slowo).equals(slowo2);
- if(czy==true){
- JOptionPane.showMessageDialog(null, "Slowo jest palindromem");
- }
- else {
- JOptionPane.showMessageDialog(null, "Slowo nie jest palindromem");
- }
- break; }
- case 2: {
- slowo=JOptionPane.showInputDialog("Podaj 1 slowo, ktore chcesz sprawdzic czy jest anagramem");
- slowo2=JOptionPane.showInputDialog("Podaj 2 slowo, ktore chcesz sprawdzic czy jest anagramem");
- slowo=slowo.toLowerCase();
- slowo2=slowo2.toLowerCase();
- ArrayList litery = new ArrayList();
- ArrayList litery2 = new ArrayList();
- for(int i=0; i<slowo.length(); i++){
- char l = slowo.charAt(i);
- Character.toLowerCase(l);
- litery.add(l);
- }
- Collections.sort(litery);
- for(int i=0; i<slowo2.length(); i++){
- char l = slowo2.charAt(i);
- Character.toLowerCase(l);
- litery2.add(l);
- Collections.sort(litery2);}
- if (litery.equals(litery2)){
- JOptionPane.showMessageDialog(null, "Slowa sa anagramami");
- }
- else {
- JOptionPane.showMessageDialog(null, "Slowa nie sa anagramami");
- }
- break;
- }
- }}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement