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. enum与typedef enum的用法

    一,两者的用法 枚举类型定义用关键字enum标识,形式为: enum 标识符 { 枚举数据表 }:枚举数据(枚举常量)是一些特定的标识符,标识符代表什么含义,完全由程序员决定.数据枚举的顺序规定了枚举 ...

  2. C语言初学者代码中的常见错误与瑕疵(13)

    https://www.cpfn.org/bbs/viewtopic.php?f=85&t=5940&sid=ccbcf716d21191452e7c08a97b502337& ...

  3. Sqlserver Sql Agent Job 只能同时有一个实例运行

    Sqlserver Sql Agent中的Job默认情况下只能有一个实例在运行,也就是说假如你的Sql Agent里面有一个正在运行的Job叫"Test Job",如果你现在再去启 ...

  4. 6lowpan

    6lowpan的产品太少,到是蓝牙smart的产品现在很多.下一步就要研究6lowpan的协议了,买了一套TI的开发套件,IBM也在卖一套6lowpan的开发套件,价格还挺贵的,带了很多sensor, ...

  5. bug的约束

    1.bug的标题:主模块-子模块-页面-功能描述-bug的描述

  6. CDN流量放大攻击思路

    首先,为了对CDN进行攻击,我们必须清楚CDN的工作原理,这里我们再来简单介绍一下CDN的工作模型. CDN的全称是Content Delivery Network(内容分发网络),通过在网络各处的加 ...

  7. C# csv 操作类

    using System.Data; using System.IO; using System.Text; namespace YanZhiwei.DotNet2.Utilities.Common ...

  8. & jobs fg Ctrl+z bg

    -l选项,jobs命令可以显示后台正在运行的任务的进程号信息: ctrl+l组合键:将放在前台的任务挂起: bg命令将挂起的任务放在后台继续运行 [xiluhua@vm-xiluhua][~]$ sl ...

  9. Codeforces 735C:Tennis Championship(数学+贪心)

    http://codeforces.com/problemset/problem/735/C 题意:有n个人打锦标赛,淘汰赛制度,即一个人和另一个人打,输的一方出局.问这n个人里面冠军最多能赢多少场, ...

  10. java面向对象学习笔记

    1.内部类 //外部类HelloWorld public class HelloWorld{ //外部类的私有属性name private String name = "imooc" ...