Give My Text Back

标签(空格分隔): 算法


时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

To prepare for the English exam Little Ho collected many digital reading materials. Unfortunately the materials are messed up by a malware.

It is known that the original text contains only English letters (a-zA-Z), spaces, commas, periods and newlines, conforming to the following format:

  1. Each sentence contains at least one word, begins with a letter and ends with a period.

  2. In a sentence the only capitalized letter is the first letter.

  3. In a sentence the words are separated by a single space or a comma and a space.

  4. The sentences are separated by a single space or a single newline.

It is also known the malware changes the text in the following ways:

  1. Changing the cases of letters.

  2. Adding spaces between words and punctuations.

Given the messed text, can you help Little Ho restore the original text?

输入

A string containing no more than 8192 English letters (a-zA-Z), spaces, commas, periods and newlines which is the messed text.

输出

The original text.

样例输入

my Name is Little Hi.

His name IS Little ho , We are friends.

样例输出

My name is little hi.

His name is little ho, we are friends.

解析

规则:

  1. 每个标点符号之前没有空格,标点符号之后加空格;
  2. ‘.’之后表示每个句子的开始,首字母应该大写;
  3. 只有每个句子的句首字母应该大写;

代码:

import java.util.Scanner;
public class Main {
public static String dealwith(String s) {
String s1 = s.trim().toLowerCase();
char[] ch = s1.toCharArray();
ch[0] = Character.toUpperCase(ch[0]);
StringBuffer sb = new StringBuffer();
boolean flag = false;
for(int i=0; i<ch.length; i++) {
if( ch[i] <= 'z' && ch[i] >= 'A') { //正常的字符
if(flag == true) {
sb.append(Character.toUpperCase(ch[i]));
flag = false;
}
else
sb.append(ch[i]);
}
else if(ch[i] == ' ') { //如果是空格,分两种情况
if(sb.charAt(sb.length()-1) != ' ')
sb.append(" ");
else
continue;
}
else { //是标点符号
if(ch[i] == '.')
flag = true;
if(sb.charAt(sb.length()-1) == ' ') { //如果标点前有空格,则删除
sb.deleteCharAt(sb.length()-1);
sb.append(ch[i]);
sb.append(" ");
}
else {
if(i == ch.length-1) //如果是最后一个字符
sb.append(ch[i]);
else {
sb.append(ch[i]);
sb.append(" ");
}
}
} }
return sb.toString();
} public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String s = sc.nextLine();
String res = dealwith(s);
System.out.println(res);
}
}

}

Give My Text Back的更多相关文章

  1. Sublime Text 3中文乱码解决方法以及安装包管理器方法

    一般出现乱码是因为文本采用了GBK编码格式,Sublime Text默认不支持GBK编码. 安装包管理器 简单安装 使用Ctrl+`快捷键或者通过View->Show Console菜单打开命令 ...

  2. requests的content与text导致lxml的解析问题

    title: requests的content与text导致lxml的解析问题 date: 2015-04-29 22:49:31 categories: 经验 tags: [Python,lxml, ...

  3. xpath提取多个标签下的text

    title: xpath提取多个标签下的text author: 青南 date: 2015-01-17 16:01:07 categories: [Python] tags: [xpath,Pyth ...

  4. 在Sublime Text 3上安装代码格式化插件CodeFormatter

    1.了解CodeFormatter插件 在Sublime Text 3中编写代码,为了能让我们的代码格式变得漂亮整洁,需要一个能自动格式代码的插件.这里发现CodeFormatter插件不错,它能支持 ...

  5. React Native 之 Text的使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  6. UGUI Text(Label)

    环境 Unity 5.3.6f1 关于Best Fit 如果勾选了 Best Fit ,当有大量的文本填充在Text上时,那么文字是不会自动换行的. 打字机效果 在github上已有现成的:https ...

  7. sublime text 3 + python配置,完整搭建及常用插件安装

    四年的时间,一直使用EmEditor编辑器进行Python开发,之前是做面向过程,只需要将一个单独的py文件维护好即可,用着也挺顺手,但是最近在做面向对象的开发,不同的py文件中相互关联较多,感觉单纯 ...

  8. 前端工程师手中的Sublime Text

    原文地址:http://css-tricks.com/sublime-text-front-end-developers/ 我的Blog:http://cabbit.me/sublime-text-f ...

  9. Sublime Text 全程指引 by Lucida

    作者:Lucida 微博:@peng_gong 豆瓣:@figure9 博客园:@figure9 原文链接:http://zh.lucida.me/blog/sublime-text-complete ...

  10. 自定义Sublime Text的图标

    sublime text很赞,windows上最接近mac逼格的轻量编辑器,对于我这样比较喜欢格调的人来说,简直不二之选啊. 美中不足的是,看久了觉得它的图标似乎不是很上心.现在都流行扁平化了而它还停 ...

随机推荐

  1. zw版【转发·台湾nvp系列Delphi例程】HALCON TileChannels

    zw版[转发·台湾nvp系列Delphi例程]HALCON TileChannels unit Unit1;interfaceuses Windows, Messages, SysUtils, Var ...

  2. ubuntu遇到的命令

    sudo passwd root这个命令是给root用户设定密码.su root切换到root用户.sudo cp 文件 /var/www移动文件到一个目录unzip xxx.zip解压zip文件mk ...

  3. Java 入门基础

    第零章 开始学习Java 1.Java基础最重要 Java学习中,Java的基础.Java面向对象是最关键的,而一些像框架技术等都是建立在基础之上东西. 多多处理问题,积累处理问题的能力. Java框 ...

  4. javax.transaction.xa.XAException: java.sql.SQLException: 无法创建 XA 控制连接。(SQL 2000,SQL2005,SQL2008)

    javax.transaction.xa.XAException: java.sql.SQLException:无法创建 XA 控制连接.错误: 未能找到存储过程'master..xp_sqljdbc ...

  5. 161011、oracle批量插入数据

    需求:从一张表中查询数据插入到另外一张表 -- Created on 2016/10/13 by RICK declare -- Local variables here begin ') loop ...

  6. JBuilder链接sql server数据库

    加载你的jdbc的驱动             一.将jdbc驱动解压到一个指定的目录,例如:c:\sql_server_jdbc,                其中包含三个驱动文件:msbase. ...

  7. linux下的module_param()解释【转】

    转自:http://blog.csdn.net/wavemcu/article/details/7762133 版权声明:本文为博主原创文章,未经博主允许不得转载. ***************** ...

  8. Xcode 证书问题报错锦集

    Xcode因为证书问题经常报的那些错 字数495 阅读13559 评论20 喜欢47 去年才开始做 iOS开发的时候,因为证书问题 Xcode 经常报这样或那样的错,经过一年多的实践,现在看见 Xco ...

  9. Word and MediaElement

    function jmc_new_lib(){// Because we still want the script to load but not the styleswp_enqueue_scri ...

  10. Readonly和disabled的区别 display:none和visible:hidden的区别

    怎样使input中的内容为只读,也就是说不让用户更改里面的内容. <input type="text" name="input1" value=" ...