Wednesday, September 16, 2015

Numbers of Words (CPROM Uitm Arau 2015)

Write a program that is able to calculate the number of words used in a complete sentence. The program will count repeated words, as one. The program will also ignore punctuation symbols used in a sentence such as comma (,) and period (.) characters.

Input
A complete sentence.
Output 
An integer representing the number of distinctive words used in the given sentence.


Samples Input
Humpty Dumpty sat on a wall, Humpty Dumpty had a great fall.

Whether the weather is warm, whether the weather is hot, we have to put up with the weather, whether we like it or not.

Samples Output
9
16


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Solved by Afiq
import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.*;

public class Q5{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      
      String line =System.getProperty("line.separator");
      scan.useDelimiter(line);
      
      String str = scan.next();
      str = str.replace(",","").replace(".","").toLowerCase();

      String[] get = str.split(" ");
      List<String> list = Arrays.asList(get);
      Set<String> set = new HashSet<String>(list);
      
      String []newstr = new String[set.size()];
      set.toArray(newstr);
               
      System.out.print(newstr.length);
   }
}


Judge Answer in Java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
importjava.util.*;
public class wordsCountDifferent {
    public static void main(String args[]) {
        String sentence;
        ArrayListwordsAL = new ArrayList();
        Scanner in = new Scanner(System.in);
        intsame_words = 0;
        char[] tSentence = new char[1000];
        intnewWordCount = 0;
        String nSentence = " ";
        System.out.print("Enter a sentence: ");
        sentence = in .nextLine();
        for (int index = 0; index < sentence.length(); index++) {
            if (sentence.charAt(index) != ',' && sentence.charAt(index) != '.') {
                tSentence[newWordCount] = sentence.charAt(index);
                newWordCount++;
            }
        }

        nSentence = nSentence.copyValueOf(tSentence, 0, newWordCount);
        for (String word: nSentence.split(" ")) {
            wordsAL.add(word);
        }
        ArrayListdiffList = new ArrayList();
        intdiffIndex = 0;
        String word1;
        String word2 = " ";
        int flag = 0;
        for (int index = 0; index < wordsAL.size(); index++) {
            word1 = (String) wordsAL.get(index);
            if (diffIndex == 0) {
                diffList.add(diffIndex, word1);
                diffIndex++;
            } else {
                flag = 0;
                for (int counter = 0; counter < diffList.size(); counter++) {
                    word2 = (String) diffList.get(counter);
                    if (word1.equalsIgnoreCase(word2)) {

                        flag = 1;
                        counter = diffList.size();
                    }
                }
                if (flag == 0) {
                    diffList.add(diffIndex, word1);
                    diffIndex++;
                }
            }
        }
        System.out.println("Size of diffList =  " + diffList.size());
    }
}

No comments:

Post a Comment