public class A {
public static void main(String[] args) throws IOException {
String s = "raining here...";
LinkedHashMap<Character, Integer> mp = new
LinkedHashMap<>();
for
(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (mp.containsKey(c)) {
Integer in = mp.get(c);
mp.put(c, in + 1);
} else {
mp.put(c, 1);
}
}
System.out.println(mp);
}
}
22.To
find the vowels and consonants in the given String:
public class A {
public static void main(String[] args) throws IOException {
String str = "All the best";
String s = str.replace(" ", "");
String vow = "";
int
v = 0;
String con = "";
int
co = 0;
for
(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == 'a' || c == 'e' || c
== 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O'
||
c == 'U') {
vow = vow + c;
v++;
} else {
con = con + c;
co++;
}
}
System.out.println("Vowels: " + vow);
System.out.println("vowels count " + v);
System.out.println("Consonants: " + con);
System.out.println("Consonants count " + co);
}
}
ASCII Values:
---------------------
65-90-------------upper case
alphabets
97-122------------lower case
alphabets
48-57-------------digits
special charcter
23.To
find the uppercase,lowercase,digits and special characters in the given String:
public class A {
public static void main(String[] args) throws IOException {
String str = "RenuDev1235@gmail.com";
String s = str.replace(" ", "");
String cap = "";
int
ca = 0;
String small = "";
int
sm = 0;
String digit = "";
int
di = 0;
String spec = "";
int
sp = 0;
for
(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int x = c;
if (x >= 65 && x
<= 90) {
// capital letter
cap = cap + c;
ca++;
} else if (x >= 97
&& x <= 122) {
// Small letter
small = small + c;
sm++;
} else if (x >= 48
&& x <= 57) {
// digit
digit = digit + c;
di++;
} else {
// Special character
spec = spec + c;
sp++;
}
}
System.out.println("Small letters: " + small);
System.out.println("Small letters count " + sm);
System.out.println("Capital letters: " + cap);
System.out.println("Capital letters count: " + ca);
System.out.println("Digits: " + digit);
System.out.println("Digits count: " + di);
System.out.println("Special characters : " + spec);
System.out.println("Special characters count: " +
sp);
}
}
24.To
find the uppercase,lowercase,digits and special characters in the given String:
public class A {
public static void main(String[] args) throws IOException {
String str = "RenuDev1235@gmail.com";
String s = str.replace(" ", "");
String cap = "";
int
ca = 0;
String
small = "";
int
sm = 0;
String digit = "";
int
di = 0;
String spec = "";
int
sp = 0;
for
(int i = 0; i < s.length(); i++) {
char x = s.charAt(i);
if (x >='A' && x
<= 'Z') {
// capital letter
cap = cap + x;
ca++;
} else if (x >= 'a'
&& x <= 'z') {
// Small letter
small = small + x;
sm++;
} else if (x >='0'
&& x <= '9') {
// digit
digit = digit + x;
di++;
} else {
// Special character
spec = spec + x;
sp++;
}
}
System.out.println("Small letters: " + small);
System.out.println("Small letters count " + sm);
System.out.println("Capital letters: " + cap);
System.out.println("Capital letters count: " + ca);
System.out.println("Digits: " + digit);
System.out.println("Digits count: " + di);
System.out.println("Special characters : " + spec);
System.out.println("Special characters count: " +
sp);
}
}
25.To
find the uppercase,lowercase,digits and special characters in the given String:
public class A {
public static void main(String[] args) throws IOException {
String str = "RenuDev1235@gmail.com";
String s = str.replace(" ", "");
String cap = "";
int
ca = 0;
String small = "";
int
sm = 0;
String digit = "";
int
di = 0;
String spec = "";
int
sp = 0;
for
(int i = 0; i < s.length(); i++) {
char x = s.charAt(i);
if (Character.isUpperCase(x)) {
// capital letter
cap = cap + x;
ca++;
} else if
(Character.isLowerCase(x)) {
// Small letter
small = small + x;
sm++;
} else if (Character.isDigit(x))
{
// digit
digit = digit + x;
di++;
} else {
// Special character
spec = spec + x;
sp++;
}
}
System.out.println("Small letters: " + small);
System.out.println("Small letters count " + sm);
System.out.println("Capital letters: " + cap);
System.out.println("Capital letters count: " + ca);
System.out.println("Digits: " + digit);
System.out.println("Digits count: " + di);
System.out.println("Special characters : " + spec);
System.out.println("Special characters count: " +
sp);
}
}
26.To
sort the characters in the given String:
public class A {
public static void main(String[] args) {
String s = "goodday";
char[] a = s.toCharArray();
System.out.println("Before sorting");
for
(int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
for
(int i = 0; i < a.length; i++) {
for (int j = i + 1; j <
a.length; j++) {
if (a[i] > a[j]) {
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println();
System.out.println("After sorting in descending order");
for
(int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
}
}
27.To
check the given two Strings are Anagram or not:
public class A {
public static void main(String[] args) {
String
s1 = "adbc";
String s2 = "dbca";
if
(s1.length() == s2.length()) {
// To convert String into
Character array
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
// Arrays--utility class,u can
use those predefined mwthods in Array variable
Arrays.sort(c1);
Arrays.sort(c2);
// ValueOf meThod here it is
converting char array into String
String a = String.valueOf(c1);
String b = String.valueOf(c2);
if (a.equals(b)) {
System.out.println("Anagram");
} else {
System.out.println("Not anagram-Exactly
characters are not matching");
}
}
else
{
System.out.println("Not
Anagram-Length not equal");
}
}
}
28.To
check the given two Strings are Anagram or not:
public class A {
public static void main(String[] args) {
String s1 = "adbc";
String s2 = "dbca";
if
(s1.length() == s2.length()) {
// To convert String into
Character array
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
// Arrays--utility class,u can
use those predefined methods in Array variable
Arrays.sort(c1);
Arrays.sort(c2);
boolean x = Arrays.equals(c1,
c2);
if (x==true) {
System.out.println("Anagram");
} else {
System.out.println("Not anagram-Exactly
characters are not matching");
}
}
else
{
System.out.println("Not
Anagram-Length not equal");
}
}
}
29.To
check the given number is prime or not:
public class A {
public static void main(String[] args) {
int
flag = 0;
int
n = 1;
if
(n == 0 || n == 1) {
System.out.println("0 and
1 not considered as prime number");
}
else {
for (int i = 2; i <= n / 2;
i++) {
if (n % i == 0) {
flag = 1;
}
}
if (flag == 0) {
System.out.println("Given number is
prime");
} else {
System.out.println("Not prime");
}
}
}
}
30.Generate
the given pattern:
*
* *
* * *
public class A {
public static void main(String[] args) {
for
(int i = 1; i <= 3; i++) {
for (int j = 0; j < i; j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
31.Generate
the given pattern:
1
2 2
3 3 3
public class A {
public static void main(String[] args) {
for
(int i = 1; i <= 3; i++) {
for (int j = 0; j < i; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
32.Generate
the given pattern:
1
1 2
1 2 3
public class A {
public static void main(String[] args) {
for
(int i = 1; i <= 3; i++) {
for (int j = 0; j < i; j++) {
System.out.print(j+" ");
}
System.out.println();
}
}
}
33.
Generate the given pattern:
*
* *
* * *
public class A {
public static void
main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int k = 3 - i; k > 0; k--) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
34.Generate the given pattern:
*
**
***
public class A {
public static void
main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int k = 3 - i; k > 0; k--) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
No comments:
Post a Comment