-3

I have a Code,

import java.util.*;
import java.util.Scanner;
import java.util.StringTokenizer;
class CountWords{
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter string: ");
        String st=input.nextLine();
        int count=0;
        StringTokenizer stk=new StringTokenizer(st," ");
        while(stk.hasMoreTokens()){
            String token=stk.nextToken();
            count++;
        }
        System.out.println("Number of words are: "+count);
    }
}

I hava a requirement that given a input as string like "This Is the!@* text to did() madam#$ split in to words."

o/p:-

Number of words are: 10

and

I have to count the no. of words of the string by neglecting the special characters of the string and store into a table column and also store the reverse of the word in another table column like(ignoring special characters in the string)

sno    words     reverse
----   ------   --------
1       This      sihT
2       Is         sI
3       the       eht  
4       text      text 

so.... on and if there are palindromes in the string,then kept that words in separate table like

word   palindrome
----   ---------
did     did
madam   madam

Thanks in advance

user1334095
  • 87
  • 2
  • 4
  • 13

2 Answers2

0

Dealing with messy HTML is messy. Use an HTML cleaner utility, such as HtmlCleaner to do the job for you.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
0
public static void main(String[] args) {
    String str = "This is the!@* text to be in words.";
    str = str.replaceAll("[^a-zA-Z0-9 ]+", "");
    System.out.println(str);
    int len = str.split("\\s+").length;
    System.out.println(len);
}

I assume special character is other than alphabet and digits.

Output:

This is the text to be in words
8
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39