Merge remote-tracking branch 'upstream/master'

This commit is contained in:
sahilb2
2018-02-06 01:03:22 -06:00
5 changed files with 94 additions and 4 deletions

View File

@ -14,7 +14,7 @@ public class CountChar {
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.");
}
@ -24,7 +24,7 @@ public class CountChar {
* @return int: Number of characters in the passed string
* */
public static int CountCharacters(String str) {
private static int CountCharacters(String str) {
int count = 0;

View File

@ -0,0 +1,33 @@
import java.util.Scanner;
/**
*A utility to check if a given number is power of two or not.
*For example 8,16 etc.
*/
public class PowerOfTwoOrNot {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int num = sc.nextInt();
boolean isPowerOfTwo = checkIfPowerOfTwoOrNot(num);
if (isPowerOfTwo) {
System.out.println("Number is a power of two");
} else {
System.out.println("Number is not a power of two");
}
}
/**
* Checks whether given number is power of two or not.
*
* @param number
* @return boolean
*/
public static boolean checkIfPowerOfTwoOrNot(int number) {
return number != 0 && ((number & (number-1)) == 0);
}
}

View File

@ -18,8 +18,8 @@ import java.util.Scanner;
input.close();
}
public static int wordCount(String s){
if(s.isEmpty() || s == null) return -1;
private static int wordCount(String s){
if(s.isEmpty() || s == null) return 0;
return s.trim().split("[\\s]+").length;
}