S I J S S I: //handling Null
S I J S S I: //handling Null
if (s == null || s.isEmpty())
{
i = -1;
}
else {
//using hashmap..
HashMap<Character, Integer> my_hash = new HashMap<Character, Integer>();
//converting to array
char[] my_array = s.toCharArray();
for (char c : my_array)
{
if(my_hash.containsKey(c))
{//for those that repeat
my_hash.put(c, my_hash.get(c)+1);
}
else{
//for those that don't repeat
my_hash.put(c, 1);
}}
//Making a set
Set<Character> my_set = my_hash.keySet();
//for-each for my_set
for (Character ch : my_set)
{
if(my_hash.get(ch) > 1)
{
System.out.println(ch);
}
else if(my_hash.get(ch) <= 1)
{
j++;
}
}
if(j == my_hash.size())
{ //If no repetition
i = -2;
}
}
return i;
}}
#1..continuesApp
import java.util.Scanner;
public class oneMain {
public static void main(String[] args)
{ int i;
Scanner input = new Scanner(System.in);
one my_one = new one();
String s = new String();
System.out.print("Give the string: ");
s = input.nextLine();
i = my_one.dups(s);
if( i == -1 )
{
System.out.println("Invalid String! -_-");
}
else if( i == -2 )
{
System.out.println("Invalid String! -_-...with no duplicate chars.");
}}}
#1..continuesJunit Tests
#1..continuesJunit Tests
#1..continuesJunit Tests
#1..continuesJunit Tests
#1..continuesconsole's view
#1..continuesconsoles view
#1..continuesconsole's view
#1..continuesconsole's view
------------------------------------------------------
------------------------END---------------------------
------------------------------------------------------
#2Are two string anagrams of each other or not?
Class
import java.util.*;
public class two
{
public static void testAnagram(String s1, String s2)
{
boolean truth_value = true;
//Deleting white space characters
String s3 = s1.replaceAll("\\s", "");
String s4 = s2.replaceAll("\\s", "");
if(s3.length() != s4.length())
{
/*If s3 & s4 don't have same
length there's no point in
comparing them as they are
not anagrams...*/
truth_value = false;
}
else
{
/*Converting to character array
after changing to upper case to
eliminate case difference.. */
char[] array_one = s3.toUpperCase().toCharArray();
char[] array_two = s4.toUpperCase().toCharArray();
//Final decision..
if(truth_value)
{
System.out.println(s1+" and "+s2+" are anagrams.");
}
else
{
System.out.println(s1+" and "+s2+" are not anagrams.");
}
}}
#2App
import java.util.*;
public class twoMain {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
two.testAnagram(s1, s2);
}}
#2 consoles display
#2..consoles view..
------------------------------------------------------
------------------------END---------------------------
------------------------------------------------------
#3..first non repetive char..
import java.util.*;
public class threeMain {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Give the string: ");
String s = input.nextLine();
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
char c[] = s.toCharArray();
for (char ch : c) {
if (map.containsKey(ch)) {
int count = map.get(ch);
map.put(ch, count + 1);
} else {
map.put(ch, 1);
}}
for (char ch : c) {
if (map.get(ch) == 1) {
System.out.println("First non-repeative char in String \""
+ s + "\" is: " + ch);
break;}}}}
#3..continueconsoles view
#4..reverse a string..using iteration & recursion..
Class.
public class four
{
String reverse = "";
public String reverseStringWithRecursion(String str){
if(str.length() == 1){
return str;
} else {
reverse += str.charAt(str.length()-1)
+reverseStringWithRecursion(str.substring(0,str.length()-1));
return reverse;}}
public void reverseStringWithIteration(String str)
{ System.out.println("With itertion: ");
for(int i=1; i<=str.length() ;i++)
{ System.out.print(str.charAt(str.length()-i)); }}}
#4..continues..App
import java.util.*;
public class fourMain {
public static void main(String[] args)
{ Scanner input = new Scanner(System.in);
four my_four = new four();
System.out.print("Enter the string: ");
String s = new String();
s = input.nextLine();
//with iteration
my_four.reverseStringWithIteration(s);
//with recursion
System.out.print("\nWith recursion: \n"+my_four.reverseStringWithRecursion(s)); }}
#4..continues..Consoles view
#5String is of only numbers or not
import java.util.*;
public class five {
public static void main(String[] args)
{ int a = 1;
Scanner input = new Scanner(System.in);
System.out.print("Give the string: ");
String s = input.nextLine();
try
{
Integer.parseInt(s);
}
catch(NumberFormatException nfEx)
{
a = -1;
}
if(a == 1)
{
System.out.println("The is string of only numbers." );
}
else
{
System.out.println("The string is not only of numbers." );
}}}
#5... continue..consoles face
#6
import java.util.*;
public class six
{
public static void dupTest(String s)
{
//using hashmap..
HashMap<Character, Integer> my_hash = new HashMap<Character, Integer>();
//converting to array
char[] my_array = s.toCharArray();
//Making a set
Set<Character> my_set = my_hash.keySet();
six.dupTest(s);}}
#6..consoles face
#7
import java.util.Scanner;
public class seven {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String mystring = new String();
char[] string_array;
int count4vowels = 0, count4consonants = 0;
string_array = mystring.toCharArray();
------------------------------------------------------
------------------------END---------------------------
------------------------------------------------------
#8 Class.
public class eightClass {
public static int[] toInt(String intString)
{
int numbedString[] = {0 , 0};
/*I took an int array of size 2.
The index [0] will have the stringed interger's value.
The index [1] will tell whether string was legit or not.
If [1] = 1, String is okay.
If [1] = -1, String is invalid.*/
try
{
numbedString[0] = Integer.parseInt(intString);
}
catch(NumberFormatException nfEx)
{
numbedString[1] = -1;
return numbedString;
}
numbedString[1] = 1;
return numbedString;
}
#8 continuesApplication.
import java.util.*;
public class eight {
public static void main(String[] args)
{ int[] stringed_int;
Scanner input = new Scanner(System.in);
String int_String = new String();
------------------------------------------------------
------------------------END---------------------------
------------------------------------------------------
#9
import java.util.Scanner;
public class ninee {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String initial_string = new String();
String final_string = new String();
String find = new String();
String replace = new String();
}
}
#9 continuesConsoles view.
------------------------------------------------------
------------------------END---------------------------
------------------------------------------------------