Skip to content

Instantly share code, notes, and snippets.

@codedeman
Created August 9, 2019 07:53
Show Gist options
  • Save codedeman/4c0ddba7a921d9f434593d268dceacd1 to your computer and use it in GitHub Desktop.
Save codedeman/4c0ddba7a921d9f434593d268dceacd1 to your computer and use it in GitHub Desktop.
count word
import java.util.Scanner;
public class ChainTest {
int count = 0;
public static final char SPACE = ' ';
public static final char TAB = '\t';
public static final char BREAK_LINE = '\n';
public void countWord(String chain)
{
for(int i = 0;i<chain.length();i++)
{
if (chain.startsWith("a")&& chain.charAt(i) != SPACE && chain.charAt(i) != TAB)
{
count += 1;
}
}
System.out.print("character is:"+chain+"the number of occurrences is:"+count);
}
public void Input()
{
String s1;
ChainTest chain = new ChainTest();
Scanner scanner = new Scanner(System.in);
System.out.print("enter a string:");
s1 = scanner.nextLine();
chain.countWord(s1);
}
public static void main(String[] args)
{
ChainTest chain = new ChainTest();
chain.Input();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment