Explanation::
- Code in Java is given below.
- Both functions are implemented and tested.
- Each and every step is explained well in code itself using comments.
- Read all the comments for better understanding of the code.
- Output screenshot is provided below at the end of code.
- Both functions are declared as static function so that there is no need to create object of class.
- Note class name is StringFrequency so save the file as StringFrequency.java
Code in java::
import java.util.*;
public class StringFrequency{
public static void main(String[] args){
/*
* Here we ask user to enter the string s
*/
Scanner sc=new Scanner(System.in);
System.out.println(“Please enter string s ::”);
String s=sc.nextLine();
/*
* Now calling the method getLetterFreq().
* Note that we have passed string s as parameter.
*/
System.out.println(“nFrequency of each character in string s is ::”);
getLetterFreq(s);
/*
* CALLING the function StrToTokens();
*/
System.out.println(“nTokens in string s are ::”);
StrToTokens(s);
}// main ends here
public static void getLetterFreq(String s){
/*
* Converting string s to lower case for simplicity.
* We store count of lower alphabets in integer array given below.
*/
String s1=s.toLowerCase();
/*
* An integer array named count is created of size 26
* for 26 characters of alphabets.
* At index 0 count of character ‘a’ is stored.
*/
int count[]=new int[26];
/*
* Now we traverse through string s1 which is lower case of string s.
* char ‘a’ has value 97 in ascii. So we subtract 97 from each alphabet.
* After subtracting we get index where we need to increment the value
* in count array.
*/
/*
* Integer variable max is used to store index of largest ascii
* value of alphabet for example suppose s=”abcdq zsed” then
* max will store 122 i.e ascii value of z.
* This variable is just used for printing formatting of last character
* to appear in output. See below for better understanding
*/
int max=0;
for(int i=0;i<s1.length();i++){
int asciiValue=(int)s1.charAt(i);
/*
* Now first we need to check if current character is alphabet or
* any delimiter like character space, commas(,) or period(.)
* If asciiValue is in the range of 97 to 122 (both inclusive),
* then it is alphabet.
*/
if(asciiValue>=97 && asciiValue<=122){
if(asciiValue>max){
max=asciiValue;
}
/*
* Suppose current char is ‘c’ then its asciiValue is 99
* So to increment count of c in array count[],
* we subtract 97 from 99 and we get 2 as index where we need to
* increment the value of count[2]++;
* Note:: By default array has values 0 at start.
*/
count[asciiValue-97]++;
}
}// For loop ends.
/*
* Here we just print the array count[] with corresponding character.
* We skip index of array where the values are 0 i.e no occurrences
* of the character.
*/
/*Here we will use the variable max*/
max=max-97;
System.out.print(“{“);
for(int i=0;i<26;i++){
/*
* First we check if count[i] is greater then 0 or no.
*/
if(count[i]>0 && i!=max){
System.out.print(“‘”+(char)(i+97)+”‘:”+count[i]+”, “);
}else if(count[i]>0 && i==max){
/*
* Purpose of max was just to not print comma(,)
* at the end of the final count value!!
*/
System.out.print(“‘”+(char)(i+97)+”‘:”+count[i]);
}
}
System.out.println(“}”);
}// getLetterFreq() function ends here
public static void StrToTokens(String s){
/*
* Creating ArrayList named tokens.
*/
ArrayList<String> tokens=new ArrayList<String>();
/**
* To get the tokens based on the delimiters like
* space, comma(,) and period(.), we traverse through
* the string using a while loop and runs a for loop inside
* while loop to traverse string s until we get any delimiter.
* So while loop hold current index and for loop ends where there
* is delimiter. See below for better understanding.
*/
/*
* An integer variable named index is declared and initialized to 0.
* The while loop runs until index is less then length of string s.
*/
int index=0;
while(index<s.length()){
/*
* The for loop starts from index variable and goes
* upto length of string.
*/
int i;
for(i=index;i<s.length();i++){
/*
* Now suppose in for loop current character is any delimiter
* then we do following.
*/
char ch=s.charAt(i);
if(ch==’ ‘ || ch==’,’ || ch==’.’){
/*
* A new string named currentToken is declared.
* It is intialized with using substring of string s.
* substring() function takes two input index’s of string s.
* first index tells from where to start the substring and
* Second index tells where to end(excluding itself.)
* For example string s=”abcdefg” the substring(2,4) will give
* string currentToken=”cd”. Note that charater at index 4 is not
* included.
*/
String currentToken=s.substring(index,i);
if(currentToken.length()>0){
/*
* We only add tokens which have length greater then 0.
*/
tokens.add(currentToken);
}
/*
* Here we increment the index.
* inilially index is 0, but when we get any delimeter
* then index get updated to next position of delimiter.
*/
index=i+1;
/*
* For loop is broken and while loop condition is checked.
*/
break;
}
}//for loop ends here
if(i==s.length()){
/*
* If there is no delimiter at end of string.
*/
tokens.add(s.substring(index,i));
/*Break the while loop finally*/
break;
}
/*
* THIS PROCESS REPEATS UNTIL index < s.length().
*/
//System.out.println(“Index::”+index);
}
/*
* Finally we just print the tokens by traversing through
* ArrayList tokens using for loop.
*/
for(int i=0;i<tokens.size();i++){
System.out.println(tokens.get(i));
}
}//Function StrToTokens() ends here
}//class ends here
Output::
Test case 1::

Test case 2::
