Skip to content

Update countwords.java #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 1, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 17 additions & 24 deletions countwords.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,23 @@
* You enter a string into this program, and it will return how
* many words were in that particular string
*
* @author Unknown
* @author Marcus
*
*/
class CountTheWords
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removed the class?

{
/**
* The main method
*
* @param args Command line arguments
*/
public static void main(String args[])
{
System.out.println("Enter the string");
Scanner sc = new Scanner(System.in);
String s=sc.nextLine();
int count = 1;
for (int i = 0; i < s.length()-1; i++)
{
if((s.charAt(i) == ' ') && (s.charAt(i+1) != ' '))
{
count++;
}
class CountTheWords{

public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter your text: ");
String str = input.nextLine();

System.out.println("Your text has " + wordCount(str) + " word(s)");
input.close();
}

public static int wordCount(String s){
if(s.isEmpty() || s == null) return -1;
return s.trim().split("[\\s]+").length;
}
System.out.println("Number of words in the string = "+count);
sc.close();
}
}

}