Sie sind auf Seite 1von 22

#1Duplicates in a stringdealing with null, empty,

with no repetitive chars string


Class..
import java.util.*;
public class one
{
public int dups(String s)
{ //Handling null
int i = 1;
int j = 0;

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..continuesjunit test
#1..continuesconsoles view

#1..continuesconsole's view
#1..continuesconsole's view

#1..continuesconsole's viewempty string

------------------------------------------------------
------------------------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();

//Sorting both arrays..


Arrays.sort(array_one);
Arrays.sort(array_two);

//Testing their equality


truth_value = Arrays.equals(array_one, array_two);
}

//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);

System.out.print("Give string 1: ");


String s1 = input.nextLine();
System.out.print("Give string 2: ");
String s2 = input.nextLine();

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

------------------------------------------------------
------------------------END---------------------------
------------------------------------------------------
#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


#5... continue.. consoles face

------------------------------------------------------
------------------------END---------------------------
------------------------------------------------------
#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();

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 +" : "+ my_hash.get(ch));
}}}}
#6continue..sixApp
import java.util.*;
public class dd {
public static void main(String[] args)
{

Scanner input = new Scanner(System.in);

System.out.print("Enter the string: ");


String s = new String(input.nextLine());

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;

System.out.print("Give the string: ");


mystring = input.nextLine();

string_array = mystring.toCharArray();

for(int count = 0; count < mystring.length(); count++)


{
if(string_array[count] == 'A' || string_array[count] == 'a')
{
count4vowels++;
}
else if(string_array[count] == 'E' || string_array[count] == 'e')
{
count4vowels++;
}

else if(string_array[count] == 'I' || string_array[count] == 'i')


{
count4vowels++;
}
else if(string_array[count] == 'O' || string_array[count] == 'o')
{
count4vowels++;
}
else if(string_array[count] == 'U' || string_array[count] == 'u')
{
count4vowels++;
}
else if(string_array[count] != ' ' && string_array[count] != '.'
&& string_array[count] != '.' && string_array[count]!='('
&& string_array[count] != ')')
{
count4consonants++;
}}
System.out.print(count4vowels+" vowels and "+count4consonants+" consonants");
}}
#7 continues..consoles display.

------------------------------------------------------
------------------------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();

System.out.print("Enter the integer string: ");


int_String = input.next();

//converting to integer by calling toInt method


stringed_int = eightClass.toInt(int_String);

/*[1] = 1 indicates that the string is valid so


go for displaying the value in index [0]......*/
if(stringed_int[1] == 1)
System.out.print("The int with integer string is: "+stringed_int[0]);

/*[1] = -1 indicates that the string is invalid so


go for displaying an error message.............*/
else if(stringed_int[1] == -1)
System.out.print("Error! Invalid String.");
}}
#8 continuesJunit testing.Validation of a wrong case.
#8 continuesJunit testing.
Validation a true case.
#8 continuesJunit Testing.
Validation of another wrong case..

#8 continuesAn invalid string of alphabets.


#8 continues

#8 continuesInteger overflow case.

------------------------------------------------------
------------------------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();

System.out.print("Give the string: ");


initial_string = input.nextLine();

System.out.print("Give the character you wanna replace: ");


find = input.nextLine();

System.out.print("Give the character you wanna replace with: ");


replace = input.nextLine();

final_string = initial_string.replaceAll(find, replace);

System.out.print("The modified: "+final_string);

}
}
#9 continuesConsoles view.

------------------------------------------------------
------------------------END---------------------------
------------------------------------------------------

Das könnte Ihnen auch gefallen