Patrick B. answered 10/06/20
Math and computer tutor/teacher
import java.io.*;
class LabProgram
{
private String str;
private char chTargetChar;
public String GetStr() { return(str); }
public char GetTargetChar() { return(chTargetChar); }
public void SetStr( String s) { str = new String(s); }
public void SetTargetChar( char ch) { chTargetChar = ch; }
public LabProgram()
{
str=null;
chTargetChar = '?';
}
public void Input()
{
Console console = System.console();
String inbuff = null;
System.out.print(" Please input the String :>");
inbuff = console.readLine();
str = new String(inbuff);
System.out.print(" Please input the character :>");
inbuff = console.readLine();
chTargetChar = inbuff.charAt(0);
}
// returns the # of instances of chTargetChar in String str
public int CountChars()
{
int iCountReturn=0;
if (str!=null)
{
int N = str.length();
for (int iLoop=0; iLoop<N; iLoop++)
{
if (chTargetChar == str.charAt(iLoop))
{
iCountReturn++;
}
}
}
return(iCountReturn);
}
public static void main(String args[])
{
LabProgram x = new LabProgram();
x.Input();
String str = x.GetStr();
char chTarget = x.GetTargetChar();
System.out.println(" There are " + x.CountChars() + " appearances of " + chTarget + " in string " + str);
x.SetStr("The quick brown fox jumped over the lazy dogs");
for (char chLoop='a'; chLoop<='z'; chLoop++)
{
x.SetTargetChar(chLoop);
System.out.println(" There are " + x.CountChars() + " appearances of " + chLoop + " in string " + str);
}
} //main