-
Notifications
You must be signed in to change notification settings - Fork 19.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c59fc92
commit c805437
Showing
2 changed files
with
19 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,15 @@ | ||
package com.thealgorithms.others; | ||
|
||
import java.util.Scanner; | ||
|
||
public class CountChar { | ||
|
||
public static void main(String[] args) { | ||
Scanner input = new Scanner(System.in); | ||
System.out.print("Enter your text: "); | ||
String str = input.nextLine(); | ||
input.close(); | ||
System.out.println( | ||
"There are " + CountCharacters(str) + " characters." | ||
); | ||
} | ||
|
||
/** | ||
* Count non space character in string | ||
* | ||
* @param str String to count the characters | ||
* @return number of character in the specified string | ||
*/ | ||
private static int CountCharacters(String str) { | ||
|
||
public static int CountCharacters(String str) { | ||
return str.replaceAll("\\s", "").length(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.thealgorithms.others; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class CountCharTest { | ||
|
||
@Test | ||
void testCountCharacters(){ | ||
String input = "12345"; | ||
int expectedValue = 5; | ||
|
||
assertEquals(expectedValue, CountChar.CountCharacters(input)); | ||
} | ||
|
||
} |