单词拼写检查

【问题描述】

已知有一个正确词汇表(存在当前目录下的文件words.txt中),编写程序,利用该词汇表对某一英文文章(存在当前目录下的文件in.txt中)进行单词正确性检查。文章中的单词是指文章中只由(小写或大写)英文字母组成的字符串。若文章中的单词在词汇表中能查找到(大小写无关,且完全相同),则该单词拼写正确,否则拼写错误。将文章中所有拼写错误的单词输出到标准输出,输出时拼写错误单词中的字母都转换为小写字母,并且按照字典顺序由小到大输出。假设所有错误单词转变为小写字母形式后,不会出现相同的错误单词;词汇表中的单词个数不会超过100个,词汇表及文章中每个单词的字符个数不会超过20,出错的单词个数不会超过50个。若没有出错单词,则什么都不输出。

【输入形式】

英文文章和词汇表分别存储在当前目录下的文件in.txt和words.txt中。词汇表中的每个单词都独占一行。

【输出形式】

以字典顺序由小到大向控制台输出错误单词,每个错误单词独占一行。

【输入样例】

假如in.txt中的文章内容如下:

C was originally designd for and implemented on the UNIX(or LINUX) operating system on the DEC PDP-11, by Dennis Ritchie.
The book is not aa introductry programing mannual.

给定的词汇表words.txt中的内容如下:
an
and
book
by
c
dec
dennis
designed
for
implemented
introductory
is
linux
manual
not
on
operating
or
originally
pdp
programming
ritchie
system
the
unix
was

【输出样例】

aa
designd
introductry
mannual
programing

【样例说明】

将读入的英文文章中的单词与给定的词汇表进行检查发现,有五个单词aa,designd,introductry,mannual和programing是错误的,因为词汇表中没有单词与它们大小写无关匹配。

参考代码:

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

import java.util.Set;

import java.util.TreeSet;

public class Main {

public static void main(String[] args) throws IOException {

BufferedReader ifile = new BufferedReader(new FileReader("in.txt"));

BufferedReader iword = new BufferedReader(new FileReader("words.txt"));

Set<String> wordss = new TreeSet<String>();

Set<String> wd = new TreeSet<String>();

Scanner sc = new Scanner(System.in);

String s;

int flag=0;

String word="";

while((s=ifile.readLine())!=null) {

s = s.toLowerCase();

for(int i=0;i<s.length();i++) {

if(Character.isLowerCase(s.charAt(i))) {

    word+=(s.charAt(i)+"");

    flag=1;

  }else if(flag==1) {

          wordss.add(word);

          word="";

          flag=0;

     }

  }

      if(flag==1) {wordss.add(word);}

}

 String hh;

  while((hh=iword.readLine())!=null) {wd.add(hh);}

  for(String ch:wordss) {

   int check=0;

  for(String hg:wd){

         if(ch.equals(hg)) {check=1;break;}

    }

     if(check==0) {System.out.println(ch);}

}

iword.close();

ifile.close();

sc.close();

  }

}

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值