Pages

Total Pageviews

Sunday, June 16, 2019

Lab 8 Exercise Questions at Java Programming

import java.util.Scanner;

public class lab9exe1
{
   public static void main (String [] args)
   {
      // declaring scanner object
   Scanner input = new Scanner(System.in);
      // input string
   System.out.println(" Enter a String ");
   String s =  input.nextLine();
      // display result
      if (Palindrome (s))
   System.out.println(s+ " is a Palindrome ");
      else     
   System.out.println(s+ " is not a Palindrome ");
   }
   public static boolean checkPalindrome (String s1)
   {
      return Palindrome (s1.toLowerCase() 
                       .replaceAll( "[\\s\\pP] ", ""));
   }
                 
      // method to find palindrome
    public static boolean Palindrome (String s1)
   {
      return s1.length ()<=1  ||
      (s1.charAt (0) ==s1.charAt (s1.length() - 1) &&
      Palindrome(s1.substring(1,
                        s1.length() - 1)));
                       
     }
  }                     



import java.util.Scanner;

public class lab9exe2
{
    public static void main(String[] args)
    {
        reverseCase();
        }
       
        public static void reverseCase() {

    final Scanner KEYBOARD = new Scanner(System.in);
    String word;

    System.out.print("Enter a word or phrase to sentence its case: ");
    word = KEYBOARD.nextLine();

    System.out.println(reverseCharacterCase(word));

}


public static StringBuilder reverseCharacterCase(String word) {

    StringBuilder wordStringBuilder = new StringBuilder(word);

    for (int i = 0; i < word.length(); i++) {
        if (Character.isUpperCase(word.charAt(i))) {
            System.out.println("Upper case found");
            wordStringBuilder.setCharAt(i,Character.toLowerCase(word.charAt(i)));
        } else if (Character.isLowerCase(word.charAt(i))) {
            System.out.println("Lower case found");
            wordStringBuilder.setCharAt(i,Character.toUpperCase(word.charAt(i)));
        }
    }

    /*StringBuilder myName = new StringBuilder("domanokz");
    myName.setCharAt(4, 'x'); */

    return wordStringBuilder;

}
}


// Java program to ReverseString using StringBuilder
import java.util.*;
 
// Class of ReverseString
class lab9exe3
{
public static void main(String[] args) {
String str = "The gate to Java nirvana is near";
String[] words = str.split(" ");
String reverseString = "";
for(int i = 0; i < words.length; i++) {
reverseString += reverse(words[i]) + " ";
}
System.out.println(reverseString.trim());
}

public static String reverse(String word) {
String reverseWord = "";
for (int i = word.length() - 1; i >= 0; i--) {
reverseWord += word.charAt(i);
}
return reverseWord;
}
}


http://erdoganakbiyik.blogspot.com/
https://www.youtube.com/channel/UCDsUnmBfVdEPkcC8FlzPKcg

No comments:

Post a Comment