编程-域名反转功能

编程-域名反转功能

题目描述

  • 实现域名反转

www.baidu.com 转换为 com.baidu.www

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Test
{
public static String reverse(String str,int start,int end)
{
char s[]=str.toCharArray();
while(start<=end)
{
char t=s[start];
s[start]=s[end];
s[end]=t;
start++;
end--;
}
str=new String(s);

return str;
}
public static String reverseDomain(String str)
{
str=reverse(str,0,str.length()-1);
int index[]=new int[10];
for(int i=0;i<index.length;i++)
{
index[i]=-1;
}
int j=-1;
for(int i=0;i<str.length();i++)
{
char c=str.charAt(i);
if(c=='.')
{
j++;
index[j]=i;
}
}
index[++j]=str.length();//设置结束位置
int start=0;
int end;
for(int ind=0;ind<index.length&&index[ind]!=-1;ind++)
{
end=index[ind]-1;
str=reverse(str,start,end);
start=index[ind]+1;
}
return str;

}
public static void main(String[] args)
{

String str="www.baibaitao.com";
str=reverseDomain(str);
System.out.println(str);
}
}
打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2019-2022 Zhuuu
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信