用于替换字符串中的字符的编程逻辑是什么? [英] What is the logic to program for replacing character in a string of characters?

查看:112
本文介绍了用于替换字符串中的字符的编程逻辑是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

ace deck应该变为gsv zxv wvxp。

a由z替换,

b由y替换,

c替换为x,

等等。

包含大写字母......替换不同。

这是在技术书面测试中被问到的。有人帮我知道它的代码

For example:
"the ace deck" should become "gsv zxv wvxp".
a replaced by z,
b replaced by y,
c replaced by x,
and so on.
Incase of uppercase letters...replacement differs.
This was asked in an technical written test.Someone help me out knowing the code for it

推荐答案

众所周知,英语中有26个字符。 />


(假设z被a替换)



按照以下步骤操作:



1)将每个角色视为一个数字,例如

a为0

b为1

c为2等等。 (提示:枚举)



2)减去你从25开始的数字(不是26,因为我们从0开始)。



3)上面的结果(第2点)是你的新角色。



因为你还没有评论大写问题指定它。
As we all know, there are 26 chars in English.

(and assuming z is replaced by a)

Follow the below steps:

1) Treat each character as a number, like
a as 0
b as 1
c as 2 and so on. (Hint: Enumerations)

2) Subtract the number you get from 25 (not 26 as we are starting from 0).

3) Result of above (pt. 2) is your new character.

Can't comment on upper case problem as you have not specified it.


为必须进行评估的测试提供代码是不好的。但指出一种方法可能是允许的......

1 - 创建字符转换图(一个用于小写,另一个用于大写),或者算一个算法将一个char转换为另一个(即如果小写字符颠倒:NewChar ='z' - (25-(Char-'a'))。

2 - 创建一个循环扫描所有字符串char by char

3 - 对于每个 char检查它是否为大写,如果是则使用转换为大写,否则为小写。

4 - 替换字符串中的char

5 - 完成
Is not good to supply code for a test where you have to be evaluated. But pointing out a way could be ammissible...
1 - Create maps of characters conversion (one for lower case and the other for uppercase), or figure an algorithm to convert one char to the other (i.e. if lowercase characters are reversed: NewChar = 'z' - (25-(Char-'a')).
2 - Create a loop scanning all your string char by char
3 - For each char check if it is uppercase, if yes use the conversion for uppercase, else for lowercase.
4 - replace the char in the string
5 - Done


这篇关于用于替换字符串中的字符的编程逻辑是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆