Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.HashMap;
- public class prac1 {
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter a sentence: ");
- String sentence = sc.nextLine();
- String word[] = sentence.toLowerCase().split(" ");
- HashMap<String, Integer> wordcount = new HashMap<>();
- for(String w:word)
- {
- w = w.replaceAll("[.,!?]","" );
- if (wordcount.containsKey(w))
- {
- wordcount.put(w,wordcount.get(w)+1);
- }
- else{
- wordcount.put(w,1);
- }
- }
- System.out.println("Enter the word you want to find the frequency of: ");
- String searchWord = sc.nextLine().toLowerCase();
- searchWord = searchWord.replaceAll("[,.!?]", "");
- if (wordcount.containsKey(searchWord))
- {
- System.out.println("Frequency of '" + searchWord + "' = " + wordcount.get(searchWord));
- }
- else
- {
- System.out.println("The word '" + searchWord + "' is not found in the sentence.");
- }
- }
- }
- import java.util.Scanner;
- import java.util.HashMap;
- public class hashmap {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter a sentence: ");
- String sentence = scanner.nextLine();
- String[] words = sentence.toLowerCase().split(" ");
- HashMap<String, Integer> wordCount = new HashMap<>();
- for (String word : words) {
- word = word.replaceAll("[.,!?]", "");
- if (wordCount.containsKey(word)) {
- wordCount.put(word, wordCount.get(word) + 1);
- } else {
- wordCount.put(word, 1);
- }
- }
- System.out.println("Word counts:");
- for (String word : wordCount.keySet()) {
- System.out.println(word + " = " + wordCount.get(word));
- }
- scanner.close();
- }
- }
- import java.util.Scanner;
- public class mat {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- // Input size of matrices
- System.out.print("Enter number of rows for first matrix: ");
- int rows1 = sc.nextInt();
- System.out.print("Enter number of columns for first matrix: ");
- int cols1 = sc.nextInt();
- System.out.print("Enter number of rows for second matrix: ");
- int rows2 = sc.nextInt();
- System.out.print("Enter number of columns for second matrix: ");
- int cols2 = sc.nextInt();
- // sc.close();
- // Check if multiplication is possible
- if (cols1 != rows2) {
- System.out.println("Matrix multiplication is not possible!");
- return;
- }
- int[][] matrix1 = new int[rows1][cols1];
- int[][] matrix2 = new int[rows2][cols2];
- int[][] result = new int[rows1][cols2];
- // Input elements for first matrix
- System.out.println("Enter elements of first matrix:");
- for (int i = 0; i < rows1; i++) {
- for (int j = 0; j < cols1; j++) {
- matrix1[i][j] = sc.nextInt();
- }
- }
- // Input elements for second matrix
- System.out.println("Enter elements of second matrix:");
- for (int i = 0; i < rows2; i++) {
- for (int j = 0; j < cols2; j++) {
- matrix2[i][j] = sc.nextInt();
- }
- }
- // Matrix multiplication
- for (int i = 0; i < rows1; i++) {
- for (int j = 0; j < cols2; j++) {
- for (int k = 0; k < cols1; k++) {
- result[i][j] += matrix1[i][k] * matrix2[k][j];
- }
- }
- }
- // Print result
- System.out.println("Resultant Matrix:");
- for (int i = 0; i < rows1; i++) {
- for (int j = 0; j < cols2; j++) {
- System.out.print(result[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement