(1)计算这篇短文的字符数(含空白)共有多少?
(2)若不含空白,这篇短文共有多少个字符?
(3)统计这篇短文用了多少个单词“to”。
(4)将这篇短文内所有大写字母改成小写,并将更改后的短文写到文本文件”joke.txt” 里。
There was an American couple who had no children, so they wanted to adopt a child. Finally, an orphanage contacted them, saying, "We have a baby for adoption.It's a Russian orphan." The couple was delighted and went to bring the baby home. 
On the way home, they stopped by a university to enroll in a Russian course. "Why do you want to learn Russian? The English that we speak is a very good language," the university secretary asked."Well, we just adopted a Russian baby. When he begins to speak Russian in a few years, we are afraid that we might not be able to understand him," the couple replied.

解决方案 »

  1.   

    读入文件,组成一个字符串str
    字符串长度:str.length()
    String[] arr = str.split(" ")
    for循环遍历arr
      可以统计非空白字符数及有多少个to最后toUpperCase()转,输出
       
      

  2.   


    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class T {
    public static void main(String[] args) throws IOException {
    //读入文件
    FileInputStream fis = new FileInputStream(new File("data.txt"));
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    StringBuffer sb = new StringBuffer();
    String str = br.readLine();
    while(str != null) {
    sb.append(str);
    str = br.readLine();
    }
    br.close();

    //统计文件的字符数、非空白字符数和单词to的个数
    str = sb.toString();
    int count = str.length();//总共的字符数
    String[] words = str.split(" ");
    int count1 = 0;//非空白字符数
    int count2 = 0;//to的个数
    for(String word : words) {
    count1 += word.length();
    if(word.equals("to")) {
    count2 ++;
    }
    }

    System.out.println("文件中总共有" + count + "个字符!");
    System.out.println("文件中总共有" + count1 + "个非空白字符!");
    System.out.println("文件中总共有" + count2 + "个单词to!");
    //文件中的所有大写字母改成小写字母
    str = str.toLowerCase();

    //转换后的内容写入文件
    FileOutputStream fos = new FileOutputStream("joke.txt");
    fos.write(str.getBytes());
    fos.close();
    }
    }
      

  3.   

     
    1.首先算出短文的字符,接住用.trim()得到新的字符串 ~前减后。得到空白
    2.1中有了
    3.简单的算法
    4.。。
     不知道行不行呢