count word in string no matter the delimiter java
public static void main(String[] args)
{
//Scanner object instantiation
Scanner dude = new Scanner(System.in);
//variable declaration
String string1 = "";
int count = 0;
boolean isWord = false;
//user prompt and input
System.out.println("Enter in your string");
string1 = dude.nextLine();
int endOfLine = string1.length()-1;
char ch [] = string1.toCharArray();
for (int i = 0; i < string1.length(); i++)
{
if(Character.isLetter(ch[i]) && i != endOfLine)
{//if character is letter and not end of line
isWord = true; //it is part of a word
}
if (!Character.isLetter(ch[i]) && isWord)
{ //if character is not a letter, and previous
//character is a letter i.e. non-letter is
//preceded by character
count++; //add to word count
isWord = false; //get ready to detect new word
}
if (Character.isLetter(ch[i]) && i == endOfLine)
{ //if character is letter
//and at end of line
count++; //add to word count
isWord = false;
}
}
System.out.println("There are " +count+ " words");
}
Also in Java:
- get raondom from array java
- java regex replace all characters before
- java nextpermutation
- string to double java exception
- expression régulière seulement un espace java
- java 8 list stream delete by name
- read lines of file randomly java
- spring iterate
- boolean parse jtextfield
- java 8 iterating and manipulating list
- how to copy all files and subdirectories in directory in java
- select photo from camera android
- how to remove all items from alist in java
- jquery set data attribute value
- java unit test an api
- java script dynamic varibale creation
- delete element from arraylist
- action on long press of edit edittext
- how to set a windows background image tkinter
- while loops java
- how to get length of integer in java
- when super add by constructor in java
- java "->"
- Matlab java.lang.IllegalArgumentException: Invalid range