所以我想将字符串=“Deepak Pundir”排列成“Dpeuenpdaikr”这样的顺序,方法是先取名字的第一个字符,然后是姓氏的第一个字符,然后是名字的第二个字符,然后是姓氏的第二个字符,依此类推。
最佳答案
这是一个代码片段,向您展示了执行您想要的操作的基本算法:
String[] components = name.split(" "); // get the first and last names
if(components.length < 2) return; // check for valid string
StringBuilder build = new StringBuilder();
for(int x = 0;x < components[0].length && x < components[1].length;x++)
build.append(components[0].charAt(x) + components[1].charAt(x));
String result = build.toString();
对于C来说,这个算法非常相似。您只需要使用 strtok 而不是 split:
- strtok :从字符串中获取标记
关于java - 名字的第一个字符,姓氏的第一个字符,然后是名字的第二个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29216077/