Example Count for same type of character in String using array :
public class countAlphabets{
public static void main(String[] args){
char[] chars = "Hello World".toLowerCase().toCharArray();
int[] count = new int[26];
for(int i = 0; i< chars.length;i++)
{
if(chars[i] <= 122 && chars[i] >= 97)
{
count[(chars[i]-97)]++;
}
}
for(int i = 0; i < 26; i++)
{
if(count[i] != 0)
System.out.println(((char)(i+97)) + "\t" + count[i]);
}
}
Example to remove spaces between a String:
String myString = "Hello World";
public class countAlphabets{
public static void main(String[] args){
char[] chars = "Hello World".toLowerCase().toCharArray();
int[] count = new int[26];
for(int i = 0; i< chars.length;i++)
{
if(chars[i] <= 122 && chars[i] >= 97)
{
count[(chars[i]-97)]++;
}
}
for(int i = 0; i < 26; i++)
{
if(count[i] != 0)
System.out.println(((char)(i+97)) + "\t" + count[i]);
}
}
Example to remove spaces between a String:
String myString = "Hello World";
myString =myString.replaceAll(" ","");
or
myString = "Hello World".replaceAll("\\s+","");
System.out.println("After remove space myString::"+myString);