Practice Set Solution - V2
Practice Set Solution - V2
Ans:
public static void main(String[] args) {
String s = "abcdef";
String rev="";
}
System.out.println("The reversed String is "+rev);
Ans:
flag = 1;
if (i != (s.length() - 1))
continue;
if (flag == 1) {
flag = 0;
}
String s = "abcdefabcdef";
int count = 0;
if(hm.containsKey(charvalue))
{
hm.put(charvalue, hm.get(charvalue)+1);
}
else
{
hm.put(charvalue, 1);
}
}
System.out.println(hm);
if(hm.containsKey(ch))
{
hm.put(ch, hm.get(ch)+1);
}
else
{
hm.put(ch, 1);
}
System.out.println(hm);
Set<Entry<Character, Integer>> kv =
hm.entrySet();
if(value>=3)
{
System.out.println(key);
}
}
}
Output:
{a=1, s=5, d=3, h=5, j=2, k=3}
s
d
h
k
5. WAP to count a pattern to be available in a given String
OR
Matcher m = p.matcher(s);
while(m.find())
{
count++;
}
System.out.println(count);
OR
String s = "jhdsaddkjsaddbasdsadbahdfhddba";
int end = 3;
for (int start = 0; end <= s.length(); end++)
{
String s1 = s.substring(start, end);
sarray[start] = s1;
start++;
LinkedHashSet<Character> hs = new
LinkedHashSet<Character>();
System.out.println(hs);
OR
public static void main(String[] args) {
String s = "abcdefabcdef";
uniquestr= uniquestr+c;
}
}
System.out.println();
System.out.println("String having no
duplicate is: "+uniquestr);//abcdef
}
7. WAP to remove the duplicate character from String and represent the character count next to it e.g.
abcdefabcdef--- expected output a2b2c2d2e2f2
}
if (j == sz - 1) {
}
System.out.println(alphanumericalstring);
OR
if(hm.containsKey(charvalue))
{
hm.put(charvalue, hm.get(charvalue)+1);
else
{
hm.put(charvalue, 1);
}
System.out.println(hm);
}
8. WAP to reverse the complete sentence for example String s = “This is String” then the expected output
should be Reverse string = “String is This”
}
System.out.println("The numeric string is :" + numericstring);
}
System.out.println("The numeric string is :" + numericstring);
OR
String s =
"bjsjdjsajdsaj54s5dsamslkdjsa45dsadsa";
String alphabeticString = "";
if(Character.isDigit(c))
{
continue;
}
else
{
alphabeticString = alphabeticString+c;
}
System.out.println(alphabeticString);//
bjsjdjsajdsajsdsamslkdjsadsadsa
}
11. WAP to add all the number individually from the String for example if the
string is “ab5ds51s2” then output should be 5+5+1+2 = 13
}
}
System.out.println("The numeric string is :" + digit);
}
String s = "45sadasd7sdsa12sdsads8";
String num = "";
Or
public static void main(String[] args) {
String s = "as1d5ds78lk9jf";
String ss = "";
int sum = 0;
for(int i=0;i<s.length(); i++ )
{
char c = s.charAt(i);
if(Character.isDigit(c))
{
ss = ss+c;
int value = Integer.parseInt(ss);
sum = sum+value;
ss="";
System.out.println(sum);//30