1. Count Unique Characters in String in Java.
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String s1 = sc.nextLine();
- getvalues(s1);
- }
- public static void getvalues(String s1) {
- String s2 = s1.toLowerCase();
- StringBuffer sb = new StringBuffer(s2);
- int l = sb.length();
- int count = 0;
- for (int i = 0; i < l; i++) {
- count = 0;
- for (int j = i + 1; j < l; j++) {
- if (sb.charAt(i) == sb.charAt(j)) {
- sb.deleteCharAt(j);
- count++;
- j--;
- l--;
- }
- }
- if (count > 0) {
- sb.deleteCharAt(i);
- i--;
- l--;
- }
- }
- if (sb.length() == 0) {
- System.out.println(-1);
- } else
- System.out.println(sb.length()
); - }
- }
2. Java program to reverse a string
import java.util.*;
class ReverseString
{
public static void main(Stri ng args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
{
public static void main(Stri
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
original = in.nextLine();
int length = original.length ();
for (int i = length - 1 ; i >= 0 ; i--)
reverse = reverse + original.c harAt(i);
reverse = reverse + original.c
System.out.println("Reverse of the string: " + reverse);
}
}
}
}
Reverse a string in Java using StringBuffer class
class InvertString
{
public static void main(Stri ng args[])
{
StringBuffer a = new StringBu ffer("Java programming is fun");
System.out.println(a.reverse( ));
}
}
{
public static void main(Stri
{
StringBuffer a = new StringBu
System.out.println(a.reverse(
}
}
3. Java program asks the user to provide a string input, and checks it for the Palindrome String.
import java.util.Scanner;
class ChkPalindrome
{
public static void main(String args[])
{
String str, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
str = sc.nextLine();
int length = str.length();
for ( int i = length - 1; i >= 0; i-- )
rev = rev + str.charAt(i);
if (str.equals(rev))
System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");
}
}
a particular character from a string.
6.Write a Java program to count the number of words in a string?
- public class WordCount {
- static int wordcount(
String string) - {
- int count=0;
- char ch[]= new cha
r[string.length()]; - for(int i=0;i<
string.length();i++) - {
- ch[i]= string.
charAt(i); - if( ((i>0)&&(
ch[i]!=' ')&&(ch[i-1]==' ')) | | ((ch[0]!=' ')&&(i==0)) ) - count++;
- }
- return count;
- }
- public static void main(
String[] args) { - String string ="
India Is My Country"; - System.out.println(
wordcount(string) + " words.") ; - }
- }
7. Write a Java program to check whether two strings are anagram or not?
- import java.util.Arrays;
- public class AnagramString {
- 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) { - isAnagram("Keep", "
Peek"); - isAnagram("Mother In
Law", "Hitler Woman"); - }
- }
No comments:
Post a Comment