Java Programs on String

1.Write a java program to count the Number of occurrences of char in String?

[cc lang=”java”]
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
*/
[/cc]

2.Write a java program to count the number of words in a string?

[cc lang=”java”]
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
*/
[/cc]

3.Find the most repeated word in a given string in java?

[cc lang=”java”]

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 map= new HashMap();
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 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
*/

[/cc]

4.Write a java program to reverse a string?

[cc lang=”java”]

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

*/
[/cc]

5.Write a code to Convert String To Integer In Java?

[cc lang=”java”]public class StringToInteger
{
public static void main(String[] args)
{
String s = “996089”;
int i = Integer.parseInt(s);
System.out.println(i);
}
}
OUTPUT
996089
[/cc]

6.Write a code to Convert Integer To String In Java?

[cc lang=”java”]public class IntegerToString
{
public static void main(String[] args)
{
int i = 996089;
String s = Integer.toString(i);
System.out.println(s);
}
}
Output : 996089[/cc]

7.Write a code to compare two strings in Java?

[cc lang=”java”]
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*/
[/cc]

8.Write a code to remove a particular character from a string in Java?

[cc lang=”java”]
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
*
*/
[/cc]

9.Write a code to check if a String is Palindrome in Java?

[cc lang=”java”]
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 */ [/cc]

10.Write a code to swap two Strings without using a third variable?

[cc lang=”java”]
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
*
*/
[/cc]