首页 技术 正文
技术 2022年11月15日
0 收藏 733 点赞 2,649 浏览 1937 个字

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);
}
}

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898