C# Revert 单词反转字符串!『测试通过』
This function will revert string "how are you?" to "you are how?".
Please input 'STOP' to stop the console!
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace HowAreYou
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 string msg =
13 @"********************************************
14 {0}
15 ********************************************";
16 string usingMsg =
17 "Revert program.\r\n This function will revert string \"how are you?\" to \"you are how?\".\r\n Please input 'STOP' to stop the console!";
18 Console.WriteLine(msg, usingMsg);
19 string stopMark = "STOP";
20
21 string input = string.Empty;
22 do
23 {
24 input = Console.ReadLine();
25 Console.WriteLine(Revert(input));
26
27 } while (input != stopMark); // "\r\n".
28
29 }
30 // how are you?
31 // you are how?
32 static string Revert(string target)
33 {
34 if (target == null || target == string.Empty)
35 {
36 return "Error:Can not revert empty string! Please enter input like this:How are you?";
37 }
38
39 // clear blank both end!
40 char blank = ' ';
41 if (target.StartsWith(blank.ToString()))
42 target = target.Trim();
43
44 StringBuilder builder = new StringBuilder();
45
46 int i = target.Length - 1;
47 int j = target.Length - 1;
48
49 if (char.IsPunctuation(target[target.Length - 1]))
50 {
51 i = target.Length - 2;
52 j = target.Length - 2;
53 }
54
55 while (j > 0)
56 {
57 if (target[j] == blank)
58 {
59 builder.Append(target.Substring(j + 1, i - j));
60 builder.Append(blank.ToString());
61 i = j - 1;
62 }
63 j--;
64 }
65
66 builder.Append(target.Substring(j, i + 1));
67
68 if (char.IsPunctuation(target[target.Length - 1]))
69 builder.Append(target[target.Length - 1].ToString());
70
71 return builder.ToString();
72 }
73
74 }
75
76 }
77
转载于:https://www.cnblogs.com/BpLoveGcy/articles/1692051.html
C# Revert 单词反转字符串!『测试通过』相关推荐
- C语言-按照单词反转字符串(完整代码)
编程实现以单词 为单位的字符串逆序 例如 HELLO WORLD ---- WORLD HELLO 目录 实现逻辑 实现代码如下:(完整备注) 执行结果如下: 实现逻辑 当前的实现逻辑,是通过两个指 ...
- 字符串反转python 测试_Python中的反转字符串问题
按单词反转字符串是一道很常见的面试题.在Python中实现起来非常简单. def reverse_string_by_word(s): lst = s.split() # split by blank ...
- java反转字符串的方法
1.首先我们定义一个方法,用来反转字符串.用 public static void (String)方法初始化一个对象,然后使用 private static ()方法对该对象进行初始化,并检查是否有 ...
- python语言中reverse_Python 反转字符串(reverse)的方法小结
谁能跟小编解释下python的字符串反转的原理女人要明白凡事首先靠自己,不要妄想在任何一方面依赖别人. [::-1]实现翻转功能. Python 的切片功能实际上比很多程序员认为的更强大. a = m ...
- java实现单词或字符串反转
最近抽空研究java数据结构发现栈和队列的特殊用处,比如像实现单词或字符串反转这一个功能,传统的做法是,将单词切分成一个个字符,然后重新组装成一个新的单词,如果用栈来实现是不是也可以做呢? 思考一下栈 ...
- LeetCode-字符串-反转字符串中的单词 II
557. 反转字符串中的单词 III class Solution {public:string reverseWords(string s) {s+=' '; //为了让最后一个单词进入循环stri ...
- c语言单词字母翻转,C语言来实现字符串反转(只有单词反转,单词里的字母不反转)...
用C/C++来实现字符串的反转.比如:字符串"I am a good developer!"反转之后变成了"developer! Good a am I".(只 ...
- 557. 反转字符串中的单词 III
557. 反转字符串中的单词 III 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例: 输入:"Let's take LeetCode cont ...
- java小编程----反转字符串中的每一个单词
package com.henu.four;public class Test07_3 {// String str= "abc god 中国 java"(较难) 反转每个单词pu ...
最新文章
- iOS调用系统相机文字改为中文
- Kafka、ActiveMQ、RabbitMQ、RocketMQ 都有什么优点和缺点?
- python网络编程——简单例子
- 建立索引要考虑的因素
- 【视频】vue指令之v-else-if
- python csv文件参数化_Python 读取csv文件的某一列数据,并定义两个pattern,确定变化区间...
- 20135206于佳心【家庭作业汇总】
- Linux环境下rman备份的实例脚本
- android viewpager 缩放,android – 使用ViewPager实现捏缩放
- 反比例函数matlab,matlab拟合指定反比例函数,在线等
- VALSE学习(十):图像去模糊-image deblurring
- Struts,Spring,Hibernate三大框架的面试
- python pyquery 解析html数据(2)
- oracle 百分位数,oracle分析函数 percent_rank, percentile_cont, percentile_disc
- 【深度学习】Colaboratory使用:Google云盘挂载及GPU使用
- Opencv项目实战:05 物体检测
- python中的chardet模块
- 高效能人士的7个习惯
- 计算机更改虚拟内存有用吗,电脑虚拟内存有什么用(小白必知虚拟内存作用及设置技巧)...
- 无关业务:少年得到,产品创新工作坊的复盘