TO PRINT DUPLICATE CHARACTERS FROM STRING
String str="MMIMAA";
char[] chars= str.toCharArray();
Hashtable<Character,Integer> charcount
= new Hashtable<Character,Integer>();
for(int
i=0;i<chars.length;i++)
{
if(charcount.containsKey(chars[i]))
charcount.put(chars[i],
charcount.get(chars[i])+1);
else
charcount.put(chars[i], 1);
}
Set<Character> count= charcount.keySet();
for (Character ch : count)
{
// if(charcount.get(ch) > 1)
{
System.out.println(ch +" :
"+ charcount.get(ch));
}}}}
TO
COUNT THE TOTAL NUMBER OF OCCURANCES OF A GIVEN CHARACTER IN AS STRING WIHTOUT
USING ANY LOOP
String str = "This is an Example Of The
Character";
System.out.println("Length Of
String:" + str.length());
System.out.println("Length Of String
Without a :" + str.replace("a", "").length());
int charcount = str.length() -
str.replaceAll("a", "").length();
System.out.println("Occurrence Of A Char
In String: " + charcount);
TO REVERSE A STRING
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to
reverse");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
System.out.println("Reverse of entered
string is: "+reverse);
TO
REMOVE ALL WHITE SPACES FROM A STRING
String str = "India Is My Country";
String str = "India Is My Country";
//1st way
String noSpaceStr = str.replaceAll("\\s", ""); // using built in method
System.out.println(noSpaceStr);
//2nd way
char[] strArray = str.toCharArray();
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < strArray.length; i++) {
if ((strArray[i] != ' ') && (strArray[i] != '\t')) {
stringBuffer.append(strArray[i]);
} }
String noSpaceStr2 = stringBuffer.toString();
System.out.println(noSpaceStr2);
TO PRINT FIRST NON REPEATED CHARACTER FROM STRING
HashMap<Character,Integer> scoreboard =
new HashMap<>();
// build table [char -> count]
for
(int i = 0; i < word.length(); i++) {
char c
= word.charAt(i);
if
(scoreboard.containsKey(c)) {
scoreboard.put(c, scoreboard.get(c) + 1);
}
else {
scoreboard.put(c, 1);
}}
// since HashMap doesn't maintain order,
going through string again
for
(int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if
(scoreboard.get(c) == 1) {
return
c;
}
TO
CHECK IF AS STRING CONTAINS ONLY DIGITS
public boolean
containsOnlyNumbers(String str) {
//It can't contain
only numbers if it's null or empty...
if (str == null ||
str.length() == 0)
return false;
for (int i = 0; i
< str.length(); i++) {
//If we find a
non-digit character we return false.
if
(!Character.isDigit(str.charAt(i)))
return
false;
}
return
true;
}
TO COUNT NUMBER OF VOWELS AND CONSTANTS IN A STRING
String line = "This website is
aw3som3.";
int vowels = 0, consonants = 0, digits
= 0, spaces = 0;
line = line.toLowerCase();
for(int i = 0; i < line.length();
++i)
{
char ch = line.charAt(i);
if(ch == 'a' || ch == 'e' || ch ==
'i'
|| ch == 'o' || ch == 'u') {
++vowels;
}
else if((ch >= 'a'&& ch
<= 'z')) {
++consonants;
}
else if( ch >= '0' && ch
<= '9')
{
++digits;
}
else if (ch ==' ')
{
++spaces;
}
}
System.out.println("Vowels: "
+ vowels);
System.out.println("Consonants:
" + consonants);
System.out.println("Digits: "
+ digits);
System.out.println("White spaces:
" + spaces);
TO FIND ALL PERMUTATIONS OF A STRING
public static
Set<String> permutationFinder(String str) {
Set<String> perm = new
HashSet<String>();
//Handling error scenarios
if (str == null) {
return null;
} else if (str.length() == 0) {
perm.add("");
return perm;
}
char initial = str.charAt(0); // first
character
String rem = str.substring(1); // Full
string without first character
Set<String> words =
permutationFinder(rem);
for (String strNew : words) {
for (int i =
0;i<=strNew.length();i++){
perm.add(charInsert(strNew,
initial, i));
}
}
return perm;
}
TO CHECK IF STRING IS PALINDROME
String str, rev =
""; Scanner sc = new Scanner(System.in);
System.out.println("Enter
a string:");
str = sc.nextLine();
int length = str.length();
f
or ( 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");
TO REMOVE DUPLICATE CHARACTERS FROM STRING
public void duplicate_occurance(String str)
{
int
len = str.Length;
char[] c= str.ToCharArray();
StringBuilder sb = new StringBuilder();
HashSet<char> set = new HashSet<char>();
for(int i=0;i<str.Length;i++){
if(!(set.Contains(c[i])))
{
set.Add(c[i]);
sb.Append(c[i]);
}}
System.out.println sb);
}
TO CHECK IF STRING CONTAINS ANOTHER STRING
String haystack = "this is a test";
String needle1 = "Java";
int
index1 = haystack.indexOf(needle1);
if
(index1 != -1)
System.out.println("The string contains the substring " +
needle1);
else
System.out.println("The string does not contain the substring
" + needle1);
TO
SORT STRING
String original = "edcba";
int j=0;
char temp=0;
char[] chars = original.toCharArray();
for (int i = 0; i <chars.length; i++) {
for ( j = 0; j < chars.length; j++) {
if(chars[j]>chars[i]){
temp=chars[i];
chars[i]=chars[j];
chars[j]=temp;
}}}
for(int k=0;k<chars.length;k++){
System.out.println(chars[k]);
}}}
TO PRINT FIBONACCI SERIES
int num1 = 0;
int num2 = 1;
int res=num1+num2;
System.out.println (num1);
System.out.println e(num2);
System.out.println (res);
for
(int i = 4; i <= limit; i++){
num1 = num2;
num2 = res;
res = num1 + num2;
System.out.println (res);
}
TO
CHECK GIVEN NUMBER IS PRIME NUMBER
public void prime_number(int num)
{
int flag=1;
for(int i=2;i<=num/2;i++) {
int res=num%i;
if (res == 0)
{
flag = 0;
break;
}
else
flag = 1;
}
if (flag == 0)
System.out.println (" not prime
number");
else
System.out.println (" prime
number");
}
SWAP
TWO NUMBERS WITHOUT USING TEMP
int a = 10;
int b = 20;
System.out.println("value of a and b
before swapping, a: " + a +" b: " + b);
//swapping value of two numbers without using
temp variable
a = a+ b; //now a is 30 and b is 20
b = a -b; //now a is 30 but b is 10 (original
value of a)
a = a -b; //now a is 20 and b is 10, numbers
are swapped
System.out.println("value of a and b
after swapping, a: " + a +" b: " + b)
No comments:
Post a Comment