Posts

Showing posts with the label String

possible palindrome or not

Write a java program to check that string is possible to make palindrome or not......... import java.util.Scanner; public class PossiblePalindrome { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter a String : "); String str=sc.nextLine(); char[] arr=str.toCharArray(); int[] index=indexNumber(arr); boolean res=possPalin(index); if(res) { System.out.println("yes it is possible to make palindrome"); } else { System.out.println("no it is not possible to make palindrome "); } } static int[] indexNumber(char[] arr) { int index[]=new int[26]; for(int i=0;i<arr.length;i++) { char ch=arr[i]; if(ch>64 && ch<91) { index[ch-65]++; } else if(ch>=97 && ch<=122) { index[ch-97]++; } } return index; } static boolean possPalin(int arr[]) { int count=0; fo...

java program to Replace a Word in String

Write a java program to Replace a word in String : package String; import java.util.Scanner; public class ReplaceString { public static void main(String[] args) { Scanner in=new Scanner(System.in); String str="Hello <<UserName>>, How are you?"; String uname=null; do { System.out.println("Enter user name :"); uname=in.nextLine(); if(uname.length()>3 && uname.length()<11) break; else { System.out.println("Invalid user please ReEnter : "); continue; } }while(uname.length()<3 || uname.length()>10); System.out.println(uname); str=str.replaceAll("<<UserName>>", uname); System.out.println(str); } }
Write a java program to calculate each word length in sentence : import java.util.Scanner; public class CountCharInEachWord { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Sentence "); String str = sc.nextLine(); charLength(str); } static void charLength(String str) { for(int i=0;i<str.length();i++) { int k=i; String temp=""; while(i<str.length() && str.charAt(i)!=' ' ) { temp=temp+str.charAt(i); i++; } System.out.println(temp+" Length is "+(i-k)); } } }
Write a java program to change all vowels in Capital and other in small : import java.util.Scanner; public class VowelsIntoCap { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Sentence "); String str = sc.nextLine(); str = vowelsCap(str); System.out.println("After changing vowels in capital \n" + str); } private static String vowelsCap(String str) { char[] ch = str.toCharArray(); for (int i = 0; i < ch.length; i++) { if (ch[i] == 'a' || ch[i] == 'e' || ch[i] == 'i' || ch[i] == 'o' || ch[i] == 'u') { ch[i] = (char) (ch[i] - 32); } else if (ch[i] >= 'A' && ch[i] <= 'Z') { if ((ch[i] == 'A' || ch[i] == 'E' || ch[i] == 'I' || ch[i] == 'O' || ch[i] == 'U')) ch[i] = (char) (ch[i]); else { ch[i] = (char) (ch[i] + 32); } } else { ...
Write a java program to count words in a string :  import java.util.Scanner; public class CountWord { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Sentence "); String str = sc.nextLine(); int c=countWord(str); System.out.println(c); } private static int countWord(String str) { char ch[] = str.toCharArray(); int count=0; for (int i = 0; i < ch.length; i++) { if((i==0 && ch[i]!=' ' )|| (ch[i]!=' ' && ch[i-1]==' ')) { count++; } } return count; } }
Write a java program to change First char in small and other in capital :  import java.util.Scanner; public class FirstCharIntoSmall { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Sentence "); String str = sc.nextLine(); System.out.println("Before small caps \n"+str); System.out.println("After changing String "); str=initSmall(str); System.out.println(str); } private static String initSmall(String str) { char ch[] = str.toCharArray(); for (int i = 0; i < ch.length; i++) { if (i == 0 && ch[i] != ' ' || (ch[i] != ' ' && ch[i - 1] == ' ')) { if (ch[i] >= 'A' && ch[i] <= 'Z') { ch[i] = (char) (ch[i] + 32); } } else { if (ch[i] >= 'a' && ch[i] <= 'z') { ch[i] = (char) (ch[i] - 32); } } ...
Write a java program to match with word: import java.util.Scanner; public class CheckSpecifiedWord { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Sentence "); String str = sc.nextLine(); System.out.println("Enter Substring : "); String substr=sc.nextLine(); boolean rs=isMatchWord(str, substr); if(rs) { System.out.println("Matched"); } else { System.out.println("unmatched"); } } static boolean isMatchWord(String str,String substr) { int j=0; for(int i=0;i<str.length();i++) { String temp=""; while(i<str.length() && str.charAt(i)!=' ') { temp=temp+str.charAt(i); i++; } System.out.println(temp); System.out.println(substr); if(temp.equals(substr)) return true; } return false; } }
Write a java program to check two string is Anagram or not :  import java.util.Scanner; class Anagram { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter first Sentence "); String st=sc.nextLine(); int[] string1=indexNumber(st); System.out.println("Enter Second Sentence "); String st2=sc.nextLine(); int[] string2=indexNumber(st2); CompareTwoArray ct=new CompareTwoArray(); boolean rs=ct.isAnagram(string1, string2); if(rs) { System.out.println("String is anagram "); } else { System.out.println("Not a anagram"); } } static int[] indexNumber(String str) { int count[]=new int[26]; for(int i=0;i<str.length();i++) { char ch=str.charAt(i); if(ch>=65 && ch<=90) { count[ch-65]++; } else if(ch>=97 && ch<=122) { count[ch-97]++; } } return count; } } ...