Java programs on String

Java Programs on String

1.Write a java program to count the Number of occurrences of char in String?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class CharCount {
    public static void main(String[] args) {
        String str = "Hello Everyone we are learning Java";
        int count = str.length();
        String temp = str.replace("r", "");
        count = count - temp.length();

        System.out.println("Number of occurances of char in String is " + count);
    }
}Output

/*Number of occurrences of char in String is 3
*/
2.Write a java program to count the number of words in a string?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class MailList {
    public static void main(String[] args) {
        String s = "Hello hi everyone!!! ";
        String[] wordcount = s.trim().split(" ");
        System.out.println("Number of words in the string = " + wordcount.length);
    }
}

/*
Output

Number of words in the string = 3
*/
3.Find the most repeated word in a given string in java?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package danish.danish;
import java.util.HashMap;
import java.util.Set;

public class MostRepeated
{
    public static void main(String[] args)
    {
        String str = "Hello Everyone we are Hello learning Hello Java";
        String[] arr= str.split(" ");
        HashMap <String,Integer> map= new HashMap<String,Integer>();
        for(String temp : arr)
        {
            if(map.containsKey(temp))
            {
                map.put(temp, map.get(temp)+1);
            }
            else
            {
                map.put(temp, 1);
            }
        }

        int count=0;
        String word=null;

        Set<String> keys =map.keySet();

        for(String temp: keys)
        {
            if(map.get(temp)>=count)
            {
                count=map.get(temp);
                word=temp;
            }

        }
        System.out.println("Word "+word+ " is repeated "+count+" Times");

    }
}

/*
 Output
 Word Hello is repeated 3 Times
 */
4.Write a java program to reverse a string?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package danish.danish;

public class Reverse {
    public static void main(String[] args) {
        String str = "Hello";

        char[] strArray = str.toCharArray();

        for (int i = strArray.length - 1; i >= 0; i--) {
            System.out.print(strArray[i]);
        }
    }
}

/*Output
olleH

*/
5.Write a code to Convert String To Integer In Java?
1
2
3
4
5
6
7
8
9
10
11
public class StringToInteger
{
    public static void main(String[] args)
    {
        String s = "996089";
        int i = Integer.parseInt(s);
        System.out.println(i);  
    }
}
OUTPUT
996089
6.Write a code to Convert Integer To String In Java?
1
2
3
4
5
6
7
8
9
10
public class IntegerToString
{
    public static void main(String[] args)
    {
        int i = 996089;
        String s = Integer.toString(i);
        System.out.println(s);    
    }
}
Output : 996089
7.Write a code to compare two strings in Java?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package danish.danish;
public class StringCompareEmp{
   public static void main(String args[]){
      String s1 = "Hello World";
      String s2 = "hello world";
     

      System.out.println( s1.compareTo(s2) );  //case sensitive
      System.out.println(s1.equals(s2));       //case sensitive
      System.out.println(s1 == s2);            //case sensitive
     
      }
}

/*Output  
-32
false
false
0
true
true*/
8.Write a code to remove a particular character from a string in Java?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package danish.danish;

public class Main {
    public static void main(String args[]) {
        String str = "this is Java";

        System.out.println(str.replace("i", " "));
    }
}

/*
 * th s s Java
 *
 */
9.Write a code to check if a String is Palindrome in Java?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Palindrome {

    public static void main(String[] args) {

        check("abc");
        check("abcba");
        check("saaaas");
    }

    private static void check(String data) {
        boolean result = true;
        int length = data.length();
        for (int i = 0; i < length / 2; i++) {
            if (data.charAt(i) != data.charAt(length - i - 1)) {
                result = false;
                break;
            }
        }
        System.out.println(data + " is palindrome = " + result);

    }

}

/*
abc is palindrome = false
abcba is palindrome = true
saaaas is palindrome = true
 */
10.Write a code to swap two Strings without using a third variable?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class main {

    public static void main(String[] args) {

        String s1 = "One";
        String s2 = "Two";

        System.out.println("Before " + s1 + " " + s2);
        s1 = s1.concat(s2);
        s2 = s1.substring(0, s1.length() - s2.length());
        s1 = s1.substring(s2.length());
        System.out.println("After " + s1 + " " + s2);

    }

}

/*
 * Before One Two After Two One
 *
 */