Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class main {
- private static String[] KEY_WORDS = {
- "int",
- "float",
- "double",
- "long",
- "string",
- "!=",
- "==",
- "=",
- ";",
- "(",
- ")",
- "{",
- "}",
- "[",
- "]"
- };
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- System.out.print("Input the string: ");
- String str = in .nextLine();
- System.out.print("Variables count: " + count_Variables(str) + "\n");
- WordIndexer wordIndexer = new WordIndexer();
- int keyCounter=0;
- for (int index=0;index<KEY_WORDS.length;index++) {
- List<Integer> actualResult = wordIndexer.findWord(str, KEY_WORDS[index]);
- for (int a=0;a<actualResult.size();a++){
- System.out.println("Keyword here -->"+str.substring(actualResult.get(a)));
- System.out.println("Keyword: "+KEY_WORDS[index]);
- System.out.println();
- }
- keyCounter+=actualResult.size();
- }
- System.out.println("Number of kyewords: " +keyCounter);
- }
- public static class WordIndexer {
- public List<Integer> findWord(String textString, String word) {
- List<Integer> indexes = new ArrayList<Integer>();
- String lowerCaseTextString = textString.toLowerCase();
- String lowerCaseWord = word.toLowerCase();
- int wordLength = 0;
- int index = 0;
- while(index != -1){
- index = lowerCaseTextString.indexOf(lowerCaseWord, index + wordLength); // Slight improvement
- if (index != -1) {
- indexes.add(index);
- }
- wordLength = word.length();
- }
- return indexes;
- }
- }
- public static int count_Variables(String str) {
- Pattern p = Pattern.compile("\\$[^\\s\\,\\$]+");
- Matcher m = p.matcher(str);
- int varCount=0;
- while (m.find()) {
- varCount++;
- System.out.println("Found a variable " + m.group());
- }
- return varCount; // returns 0 if string starts or ends with space " ".
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement