Categories
Interview java String

count number of words in string using java

In this post, we will learn about count number of words in string using java

In below example we using split method to count number of words in a sentence.

If String have more than one space consecutively, then we need to replace with one string using below code

sentence.replaceAll("\\s+", " ").trim();
Syntax
properString.split(" ");
Example using split
public class CountWords {

	public static void main(String[] args) {

		try {
			String sentence = "    The pen    is mightier than the sword . ";
			String properString = sentence.replaceAll("\\s+", " ").trim();
			String[] split = properString.split(" ");
			System.out.println("The sentence have " + split.length + " words");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
Output
The sentence have 8 words
Example using StringTokenizer
import java.util.StringTokenizer;

public class CountWords {

	public static void main(String[] args) {
		try {
			String sentence = "    The pen    is mightier than the sword . ";
			StringTokenizer tokens = new StringTokenizer(sentence);
			System.out.println("The sentence have " + tokens.countTokens() + " words");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
Output
The sentence have 8 words
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/strings/CountWordsUsingSplit.java

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/strings/CountWordsUsingTokenzier.java

Related Articles

find the first occurrence of a character in string using java

Categories
Interview java matrix

multiply two matrix using java with example

In this post, we will learn about multiply two matrix in java with example

We are declaring two matrix named as a,b and one more variable mul to store the multiplication of a&b matrix

Example
public class MatrixMultiplication {

	public static void main(String[] args) {
		try {

			int a[][] = { { 50, 60 }, { 20, 15 } };
			int b[][] = { { 25, 20 }, { 10, 5 } };

			// Declaring the diff matrix
			int[][] mul = new int[a.length][b.length];

			// multiply 2 matrices
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < b.length; j++) {
					mul[i][j] = 0;
					for (int k = 0; k < 2; k++) {
						mul[i][j] += a[i][k] * b[k][j];
					}
					System.out.print(mul[i][j] + " ");
				}
				System.out.println();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
Output
1850  1300 
650   475 
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/MatrixMultiplication.java

Related Articles

subtracting two matrix in java with example

Categories
Interview java matrix

subtracting two matrix in java with example

In this post, we will learn about subtracting two matrix in java with example

We are declaring two matrix named as a,b and one more variable diff to store the subtraction of a&b matrix

Example
public class SubtractingMatrix {

	public static void main(String[] args) {
		try {

			int a[][] = { { 50, 60 }, { 20, 15 } };
			int b[][] = { { 25, 20 }, { 10, 5 } };

			// Declaring the diff matrix
			int[][] diff = new int[a.length][b.length];

			// Subtracting two matrix
			for (int i = 0; i < a.length; i++) {

				for (int j = 0; j < b.length; j++) {
					diff[i][j] = a[i][j] - b[i][j];
				}
			}

			// Printing the diff matrix
			for (int i = 0; i < diff.length; i++) {
				for (int j = 0; j < diff[i].length; j++) {
					System.out.print(diff[i][j] + " ");
				}
				System.out.println();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
Output
25 40 
10 10 
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/SubtractingMatrix.java

Related Articles

adding two matrix in java with example

Categories
Interview java matrix

adding two matrix in java with example

In this post, we will learn about adding two matrix in java with example

In below example,We are using two dimensional array for this operations.

Here we are declaring two matrix named as a,b and one more variable sum to store the addition of a&b matrix

The first for loop is used to add two matrix and the second for loop used to print the addition value

Example
public class AddingMatrix {

	public static void main(String[] args) {
		try {

			int a[][] = { { 54, 67 }, { 45, 56 } };
			int b[][] = { { 25, 56 }, { 85, 96 } };

			// Declaring the sum matrix
			int[][] sum = new int[a.length][b.length];

			// Adding two matrix
			for (int i = 0; i < a.length; i++) {

				for (int j = 0; j < b.length; j++) {
					sum[i][j] = a[i][j] + b[i][j];
				}
			}

			// Printing the sum matrix
			for (int i = 0; i < sum.length; i++) {
				for (int j = 0; j < sum[i].length; j++) {
					System.out.print(sum[i][j] + " ");
				}
				System.out.println();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
Output
79 123 
130 152 
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/AddingMatrix.java

Related Articles

create 2×2 matrix using java

Categories
Interview java

Print non repeated characters in java

Print non repeated characters in java

In this example we will print non repeated characters from a string using java 
This is mostly asked in the java programming interview 

Example

import java.util.ArrayList;

public class NonRepeatedCharcters {

  public static void main(String[] args) {
    String s = "BeginnersBug";
    String nonRepeatedChars = "";
    ArrayList arrayList = new ArrayList();
    char[] charArray = s.toCharArray();

    for (int i = 0; i < charArray.length; i++) {
      // Checking that character already exists on array list
      boolean contains = arrayList.contains(charArray[i]);
      if (!contains) {        
        arrayList.add(charArray[i]);
        nonRepeatedChars += charArray[i];
      }
    }
    System.out.println(nonRepeatedChars);
  }
}

Output

Beginrsu

Array List Reference

 https://beginnersbug.com/arraylist-in-java/

Download Source Code

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/NonRepeatedCharcters.java

Categories
Interview java

Print String reverse using java with example

Print String reverse in java

Print String in reverse using java with example. This is most asked qustion in java interviews In below example we will see that.

Example

public class StringReverseExample {

  public static void main(String[] args) {
    String s = "java";
    int stringLength = s.length();
    // In Below for loop starting will be length-1
    // The end will 0 the index
    for (int i = stringLength - 1; i >= 0; i--) {
      // we used charAt method to get value from the string
      // instead of println we used print to print on same line
      System.out.print(s.charAt(i));
    }
  }
}

Output

avaj

String reverse without for loop

public class StringReverse {

	public static void main(String[] args) {
		String s = "Java";
		// Below we used StringBuilder to print string in reverse
		StringBuilder builder = new StringBuilder(s);
		System.out.println(builder.reverse());
	}

}

Output

avaJ

To know more about StringBuilder, please refer below link
https://beginnersbug.com/stringbuilder-java/

Download

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/StringReverseExample.java