-
Notifications
You must be signed in to change notification settings - Fork 14
/
reverse.c
45 lines (41 loc) · 1000 Bytes
/
reverse.c
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
#include<stdio.h>
#include<string.h>
// 字符串翻转函数
void reverse_str(char* strInput, int nStart, int nEnd)
{
if (nStart >= nEnd || nStart < 0 || nEnd >= strlen(strInput)) {
return;
}
while(nStart < nEnd) {
char cTemp = strInput[nStart];
strInput[nStart] = strInput[nEnd];
strInput[nEnd] = cTemp;
nStart++;
nEnd--;
}
}
void reverse_domain(char* strInput)
{
reverse_str(strInput, 0, strlen(strInput) - 1);
// 域名中的每个单词反转
char* strStart = strInput;
int nStart = 0;
int nEnd = 0;
while( *strInput != '\0')
{
if ( *strInput == '.')
{
reverse_str(strStart, nStart, nEnd - 1);
nStart = nEnd + 1;
strStart = strInput;
}
nEnd ++;
strInput ++;
}
}
int main()
{
char a[] = "www.mi.com";
reverse_domain(a);
printf("%s", a);
}