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. Eclipse字符集设置方式

    默认的字符集是GBK 1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右侧Text fi ...

  2. 使用sudo执行命令的时候提示找不到命令

    事出有因, 源自使用源码编译 nginx , 在 /usr/local/sbin/ 目录下创建了nginx 启动的符号链接 , 执行 sudo nginx 的时候提示找不到命令, 但是使用普通用户身份 ...

  3. HTML5动画软件工具编辑器 HTML5动画分类 工具推荐

    接下来介绍几款制作HTML5动画的工具,它们可以分为几类: 1.导出canvas动画: Flash CC(13.1).Animation.Radi 2.导出DIV+CSS3动画: HTML5 Make ...

  4. Android多线程通信之Handler

    主线程 public class MainActivity extends ActionBarActivity { private Handler handler; // private Thread ...

  5. android 项目学习随笔十二(ListView加脚布局)

    1.ListView加脚布局 头布局initHeaderView,在onTouchEvent事件中进行显示隐藏头布局切换 脚布局initFooterView,实现接口OnScrollListener, ...

  6. Bootstrap:弹出框和提示框效果以及代码展示

    前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的 ...

  7. mount源码分析 【转】

    转自:http://blog.chinaunix.net/uid-10769062-id-3230811.html Busybox- 在util-linux/mount.c的line:1609行首先映 ...

  8. php的header()函数之设置content-type

    //定义编码 header( 'Content-Type:text/html;charset=utf-8 '); //Atom header('Content-type: application/at ...

  9. awk 手册--【转载】

    1. 前言 有关本手册 : 这是一本awk学习指引,  其重点着重于 : l         awk 适于解决哪些问题 ? l         awk 常见的解题模式为何 ? 为使读者快速掌握awk解 ...

  10. 工具类 dp转px 获取图片实际尺寸 获取屏幕尺寸

    dp转px public class Dp2pxUtils { public static int Dp2Px(Context context, float dp) { final float sca ...