mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-23 20:44:39 +08:00
docs(Others): update countwords.java and crc32.java
- By @yanglbme
This commit is contained in:
@ -1,26 +1,26 @@
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* You enter a string into this program, and it will return how
|
* You enter a string into this program, and it will return how many words were
|
||||||
* many words were in that particular string
|
* in that particular string
|
||||||
*
|
|
||||||
* @author Marcus
|
|
||||||
*
|
*
|
||||||
|
* @author Marcus
|
||||||
*/
|
*/
|
||||||
public class countwords{
|
public class CountWords {
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args) {
|
||||||
Scanner input = new Scanner(System.in);
|
Scanner input = new Scanner(System.in);
|
||||||
System.out.println("Enter your text: ");
|
System.out.println("Enter your text: ");
|
||||||
String str = input.nextLine();
|
String str = input.nextLine();
|
||||||
|
|
||||||
System.out.println("Your text has " + wordCount(str) + " word(s)");
|
|
||||||
input.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int wordCount(String s){
|
System.out.println("Your text has " + wordCount(str) + " word(s)");
|
||||||
if(s.isEmpty() || s == null) return 0;
|
input.close();
|
||||||
return s.trim().split("[\\s]+").length;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int wordCount(String s) {
|
||||||
|
if (s == null || s.isEmpty())
|
||||||
|
return 0;
|
||||||
|
return s.trim().split("[\\s]+").length;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,27 +1,29 @@
|
|||||||
import java.util.BitSet;
|
import java.util.BitSet;
|
||||||
|
|
||||||
//Generates a crc32 checksum for a given string or byte array
|
/**
|
||||||
public class crc32 {
|
* Generates a crc32 checksum for a given string or byte array
|
||||||
|
*/
|
||||||
|
public class CRC32 {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(Integer.toHexString(crc32("Hello World")));
|
System.out.println(Integer.toHexString(crc32("Hello World")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int crc32(String str) {
|
public static int crc32(String str) {
|
||||||
return crc32(str.getBytes());
|
return crc32(str.getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int crc32(byte[] data) {
|
public static int crc32(byte[] data) {
|
||||||
BitSet bitSet = BitSet.valueOf(data);
|
BitSet bitSet = BitSet.valueOf(data);
|
||||||
int crc32 = 0xFFFFFFFF; //initial value
|
int crc32 = 0xFFFFFFFF; // initial value
|
||||||
for(int i=0;i<data.length*8;i++) {
|
for (int i = 0; i < data.length * 8; i++) {
|
||||||
if(((crc32>>>31)&1) != (bitSet.get(i)?1:0))
|
if (((crc32 >>> 31) & 1) != (bitSet.get(i) ? 1 : 0))
|
||||||
crc32 = (crc32 << 1) ^ 0x04C11DB7; //xoring with polynomial
|
crc32 = (crc32 << 1) ^ 0x04C11DB7; // xor with polynomial
|
||||||
else
|
else
|
||||||
crc32 = (crc32 << 1);
|
crc32 = (crc32 << 1);
|
||||||
}
|
}
|
||||||
crc32 = Integer.reverse(crc32); //result reflect
|
crc32 = Integer.reverse(crc32); // result reflect
|
||||||
return crc32 ^ 0xFFFFFFFF; //final xor value
|
return crc32 ^ 0xFFFFFFFF; // final xor value
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user