package com.abuge;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import org.junit.Test;
/**
*
*1 字串转换
问题描述:
将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a;
若输入的字符串连续出现两个字母相同时,后一个字母需要连续转换2次。例如:aa 转换为 bc,zz 转换为 ab;
当连续相同字母超过两个时,第三个出现的字母按第一次出现算。
要求实现函数:
void convert(char *input,char* output)
【输入】 char *input , 输入的字符串
【输出】 char *output ,输出的字符串
【返回】 无
示例
输入:char*input="abcd"
输出:char*output="bcde"
输入:char*input="abbbcd"
输出:char*output="bcdcde"
* @author AbuGe
*
*/
public class InterviewDemo
{
public static void main(String[] args)
{
String input = "abbbcd";
String output= null;
convert(input, output);
}
public static void convert(String input, String output)
{
int count = 0;
//读到一行字符串,将字符串转换为字符串数组
char[] strArray = input.toCharArray();
char tmp = 0;
//对字符串进行循环转换
for(int i = 0; i < strArray.length; i++)
{
if(strArray[i] != tmp)
{
tmp = strArray[i];
strArray[i] = (char) ((strArray[i] + 1) % 97 + 'a');
count = 1;
}else
{
if(count == 1)
{
strArray[i] = (char) ((strArray[i] + 2) % 97 + 'a');
count = 0;
}else
{
strArray[i] = (char) ((strArray[i] + 1) % 97 + 'a');
count = 1;
}
}
}
output = new String(strArray);
System.out.println(output);
}
----------------------------------------------键盘录入-------------------------------------------------------------------------------------------------
@Test
public void testConvert()
{
BufferedReader bufr = null;
bufr = new BufferedReader(new InputStreamReader(System.in));
String strInput = null;
String strOutput = null;
int count = 0;
try
{
while((strInput = bufr.readLine()) != null)
{
//读到一行字符串,将字符串转换为字符串数组
char[] strArray = strInput.toCharArray();
char tmp = 0;
//对字符串进行循环转换
for(int i = 0; i < strArray.length; i++)
{
if(strArray[i] != tmp)
{
tmp = strArray[i];
strArray[i] = (char) ((strArray[i] + 1) % 97 + 'a');
count = 1;
}else
{
if(count == 1)
{
strArray[i] = (char) ((strArray[i] + 2) % 97 + 'a');
count = 0;
}else
{
strArray[i] = (char) ((strArray[i] + 1) % 97 + 'a');
count = 1;
}
}
}
strOutput = new String(strArray);
System.out.println(strOutput);
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}