Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

JAVA PROGRAMMING

Lab Assignment 2

CSE1007
SIDDHANT MAHURKAR
17BCE0668
Q.1 Write a program to check whether String is Rotational
palindrome in java
Code:
import java.util.*;
public class Main {

public static void main(String[] args) {


Scanner sc= new Scanner(System.in);
System.out.print("Enter a String: ");
String inputString;
inputString = sc.nextLine();

isRotationalPaliondrome(inputString);

public static boolean isRotationalPaliondrome(String inputString){


char ar[]=inputString.toCharArray();
for(int x=0;x<inputString.length();x++){
rotateString(ar);
if(isPalindrome(ar)){
System.out.println("YES! "+inputString+" is rotation of the
palindrome: "+String.valueOf(ar));
return true;
}
}
System.out.println("NO! "+inputString+" is not a rotational
palindrome ");
return false;
}

public static void rotateString(char[] ar){


char temp = ar[0];
int x=0;
for(x=0;x<ar.length-1;x++){
ar[x]=ar[x+1];
}
ar[x]=temp;
}

public static boolean isPalindrome(char ar[]){


for(int i=0,j=ar.length-1; i<(ar.length/2); i++,j--){
if(ar[i]!=ar[j])
return false;
}
return true;
}

Output:
Q.2 Write a Java program to check whether String is Palindrome
or not (with and without using recursion) in java
Without
Code:
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the String : ");
String a = sc.nextLine();
int n = a.length()-1;
int flag=0;
for(int i=0;i<n/2;i++)
{
if(a.charAt(i)!=a.charAt(n-i))
{
flag=1;
break;
}
}
if(flag==0)
{
System.out.println(a+" is a Palindrom");
}
else
{
System.out.println(a+" is not a Palindrom");
}
}
}

Output:

With Recursion
Code:
public static boolean Pal(String s)
{
if(s.length() == 0 || s.length() == 1)
return true;
if(s.charAt(0) == s.charAt(s.length()-1))
return Pal(s.substring(1, s.length()-1));
return false;
}

public static void main(String args[])


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String st = sc.nextLine();
if(Pal(st))
System.out.println(st + " is a Palindrome");
else
System.out.println(st + " is not a Palindrome");
}
Output:
Q.3 Write a Java Program to reverse the string using recursion
Code:
import java.util.Scanner;
public class Main {

public static void main(String[] args) {


String str;
System.out.println("Enter a string: ");
Scanner scanner = new Scanner(System.in);
str = scanner.nextLine();
scanner.close();
String reversed = reverseString(str);
System.out.println("The reversed string is: " + reversed);
}

public static String reverseString(String str)


{
if (str.isEmpty())
return str;
return reverseString(str.substring(1)) + str.charAt(0);
}
}
Output:

Q.4 . Write a program to find out first non-repeating character in


string in java
Code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter a string: ");
String str1 = sc.nextLine();
System.out.println("The given string = " + str1);
for (int i = 0; i < str1.length(); i++) {
boolean unique = true;
for (int j = 0; j < str1.length(); j++) {
if (i != j && str1.charAt(i) == str1.charAt(j)) {
unique = false;
break;
}
}
if (unique) {
System.out.println("first non repeating character in String = " +
str1.charAt(i));
break;
}
}
}
}

Output:

Q.5 Write a program to reverse words in sentence in java


Code:
import java.util.*;

public class Main


{
public static String reverseTheSentence(String inputString)
{
String[] words = inputString.split("\\s");

String outputString = "";

for (int i = words.length-1; i >= 0; i--)


{
outputString = outputString + words[i] + " ";
}
return outputString;
}

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);

System.out.print("Enter a String:");

String inputString = sc.nextLine();

String outputString = reverseTheSentence(inputString);

System.out.println("Original String : "+inputString);

System.out.println("String after reversing words in sentence:


"+outputString);

sc.close();
}
}
Output:
Q.6 Write a program to find out substring in given string
Code:
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
System.out.print("Enter the substring: ");
String substr = sc.nextLine();
int n1 = str.length();
int n2 = substr.length();
for (int i = 0; i <= n1 - n2; i++) {
int j;
for (j = 0; j < n2; j++) {
if (str.charAt(i + j) != substr.charAt(j))
break;
}
if (j == n2) {
System.out.println(str+ " contains "+substr+" at index " + i);
return;
}
}
System.out.println("The substring is not present in the string");
}
}

Output:

Q.7 Write a program to Find whether all characters of string


appear in the inputString in same order in java .
Code:
import java.util.*;
public class Main {

public static void main(String[] args) {


Scanner sc= new Scanner(System.in);
System.out.print("Enter a string: ");
String inputString=sc.nextLine();
System.out.print("Enter Substring to find: ");
String findString=sc.nextLine();
System.out.println(checkContinuity(inputString,findString) ==true ?
"TRUE":"FALSE");
}

static boolean checkContinuity(String inputString, String findString){

char inputCh[]=inputString.toCharArray();
char findCh[]=findString.toCharArray();
int pos=0;

for(int i=0;i<inputCh.length;i++){
if(inputCh[i]==findCh[pos]){
pos++;
if(pos==findCh.length)
return true;
}
}
return false;
}
}
Output:

Q.8 Write a program to find out all possible unique subString


present in a given string.
Code:
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc =new Scanner(System.in);
System.out.print("Enter a String: ");
String str=sc.nextLine();
System.out.println("All substring of "+str+" are:");
System.out.print("[");
for (int i = 0; i < str.length(); i++) {
for (int j = i+1; j <= str.length(); j++) {
System.out.print(str.substring(i,j)+" ");

}
}
System.out.print("]");
}
}
Output:

Q. 9 Write a program to find count of each character in String


and print them in java - String may consist of repeating
characters in java.
Code:
import java.util.*;
public class Main {

public static void main(String[] args) {


Scanner sc= new Scanner(System.in);
System.out.print("Enter a string: ");
String inputString=sc.nextLine();
System.out.print("Output is : ");
characterCount(inputString);
}

public static void characterCount(String inputString){

char[] inputAr=inputString.toCharArray();
int count=0,arLength;

arLength=inputAr.length;
for(int x=0; x<arLength; x++){
char ch=inputAr[x];
for(int y=x+1; y<arLength; y++){
if(inputAr[y]==ch){
for(int z=y; z< arLength-1; z++)
inputAr[z]=inputAr[z+1];
arLength--;
y=x;
}
}
}

for(int x=0;x<arLength;x++){
count=0;

for(int y=0; y<inputAr.length; y++){


if(inputAr[x]==inputString.charAt(y))
count++;
}
System.out.print(inputAr[x]+""+count+"");

}
System.out.println();

Output:
Q. 10 Write a Java program for abbreviation.
e.g.
Vellore Institute of Technology
Output:V.I.T.
Code:
import java.util.*;
public class Main {

static void Ini(String st)


{
if (st.length() == 0)
return;
System.out.print(Character.toUpperCase(
st.charAt(0)));
for (int i = 1; i < st.length() - 1; i++)
if (st.charAt(i) == ' ')
System.out.print("." + Character.toUpperCase(
st.charAt(i + 1)));
System.out.println();
}

public static void main(String args[])


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String s = sc.nextLine();
Ini(s);
}

Output:

Q. 11 Write a program to Replace space in string by $#$ in java.


entered string = java made so easy
output string = java$#$made$#$so$#$easy
Code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.print("Enter a String: ");
String input=sc.nextLine();
System.out.println("output string = "+replaceSpaces(input));

}
public static String replaceSpaces(String input) {
char ch[]=input.toCharArray();
int spaceCount = 0;
for (int i = 0; i < ch.length; i++) {
if (ch[i] == ' ') {
spaceCount++;
}
}

int newLength = ch.length + spaceCount * 2;


char chNew[]=new char[newLength] ;
for (int i = ch.length - 1; i >= 0; i--) {
if (ch[i] == ' ') {
chNew[--newLength] = '$';
chNew[--newLength] = '#';
chNew[--newLength] = '$';
}
else
chNew[--newLength] = ch[i];

}
return(new String(chNew));
}
}
Output:
Q. 12 Write a program that will help us in changing case of
characters in String in java.
Example in java>
input String: Java Made So Easy
output: jAVA mADE sO eASY
Code:
import java.util.*;
class Main {
public static void main(String args[]) {
Scanner sc =new Scanner(System.in);
System.out.print("Enter a String: ");
String inputString=sc.nextLine();
System.out.println("output : "+changecase(inputString));

static String changecase(String inputString) {


char ar[]=inputString.toCharArray();

for (int i = 0; i < ar.length; i++) {

if (ar[i]>=65 && ar[i] <=90) {


ar[i] += 32;
} else if(ar[i]>=97 && ar[i] <=122){
ar[i] -= 32;
}

}
return new String(ar);
}

}
Output:

Q. 13 How to reverse String using StringBuffer example in java


Code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.print("Enter a String: ");
String inputString=sc.nextLine();
StringBuffer sb = new StringBuffer(inputString);
System.out.println("string: " + sb);
System.out.println("reverse: " + sb.reverse());
}
}
Output:

Q. 14 Take a String and replace all vowels into Capital case in


java
Code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.print("Enter a String: ");
String str=sc.nextLine();
System.out.println("After replacing all vowels into capital case =
"+str.replaceAll("a", "A").replaceAll("e", "E").replaceAll("i",
"I").replaceAll("o", "O").replaceAll("u", "U"));
}
}

Output:

Q. 15 Java program to convert string to lowercase and uppercase


using ASCII Values and using String functions
Using ASCII Values
Code:
public class Fibo
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String s = sc.nextLine();
int n;
char c;
String z="";
for(int i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c>=97 && c<=122)
{
n=c-32;
c=(char)n;
}
z=z+c;
}
System.out.println("\nUpper Case: "+z);
z="";
for(int i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c>=65 && c<=90)
{
n=c+32;
c=(char)n;
}
z=z+c;
}
System.out.println("\nLower Case: "+z);
z="";
}
}
Output:
Code:
Using Inbuilt function
import java.util.*;
public class Fibo
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String s = sc.nextLine();
System.out.println("Upper Case : "+s.toUpperCase());
System.out.println("Lower Case : "+s.toLowerCase());
}
}
Output:

Q.16 Write a Java Program to check if two strings are anagrams


in Java:
Code:
import java.util.*;
import java.util.Arrays;

public class Main {


static void isAnagram(String str1, String str2) {
String s1 = str1.replaceAll("\\s", "");
String s2 = str2.replaceAll("\\s", "");
boolean status = true;
if (s1.length() != s2.length()) {
status = false;
} else {
char[] ArrayS1 = s1.toLowerCase().toCharArray();
char[] ArrayS2 = s2.toLowerCase().toCharArray();
Arrays.sort(ArrayS1);
Arrays.sort(ArrayS2);
status = Arrays.equals(ArrayS1, ArrayS2);
}
if (status) {
System.out.println(s1 + " and " + s2 + " are anagrams");
} else {
System.out.println(s1 + " and " + s2 + " are not anagrams");
}
}

public static void main(String[] args) {


Scanner sc =new Scanner(System.in);
System.out.print("Enter first String: ");
String str1=sc.nextLine();
System.out.print("Enter second String: ");
String str2=sc.nextLine();
isAnagram(str1,str2);

}
}
Output:

You might also like