The Wayback Machine - https://web.archive.org/web/20200519015538/https://github.com/TheAlgorithms/Java/pull/1283/files
Skip to content
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

Add char and word counters #1283

Open
wants to merge 1 commit into
base: Development
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -0,0 +1,13 @@
package com.others;

public class CountChar {
/**
* Count non space character in string
*
* @param str String to count the characters
* @return number of character in the specified string
*/
public static int countCharacters(String str) {
return str.replaceAll("\\s", "").length();
}
}
@@ -0,0 +1,37 @@
package com.others;

/**
* You enter a string into this program, and it will return how many words were
* in that particular string
*
* @author Marcus
*/
public class CountWords {
public static int wordCount(String s) {
if (s == null || s.isEmpty())
return 0;
return s.trim().split("[\\s]+").length;
}

/**
* counts the number of words in a sentence but ignores all potential
* non-alphanumeric characters that do not represent a word. runs in O(n) where
* n is the length of s
*
* @param s String: sentence with word(s)
* @return int: number of words
*/
public static int secondaryWordCount(String s) {
if (s == null || s.isEmpty())
return 0;
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isLetter(c) || Character.isDigit(c))
sb.append(c);
if (Character.isSpaceChar(c))
sb.append(c);
}
s = sb.toString();
return s.trim().split("[\\s]+").length;
}
}
@@ -0,0 +1,17 @@
package com.others;


import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CountCharTest {

@Test
void testCountChar(){
CountChar counter = new CountChar();
Assertions.assertEquals(0, counter.countCharacters(""), "Incorrect");
Assertions.assertEquals(11, counter.countCharacters("coincidence"), "Incorrect");
Assertions.assertEquals(8, counter.countCharacters("brownish"), "Incorrect");
Assertions.assertEquals(34, counter.countCharacters("thisisgoingtobeanillegallylongword"), "Incorrect");
}
}
@@ -0,0 +1,20 @@
package com.others;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CountWordsTest {

@Test
void testWordCounter(){
CountWords counter = new CountWords();
Assertions.assertEquals(0, counter.wordCount(""), "Incorrect");
Assertions.assertEquals(0, counter.secondaryWordCount(""), "Incorrect");

Assertions.assertEquals(1, counter.wordCount("coincidence"), "Incorrect");
Assertions.assertEquals(1, counter.secondaryWordCount("coincidence"), "Incorrect");

Assertions.assertEquals(7, counter.wordCount("This is a not so long sentence."), "Incorrect");
Assertions.assertEquals(7, counter.secondaryWordCount("This is a not so long sentence."), "Incorrect");
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.