23 lines
479 B
C
23 lines
479 B
C
|
#include <stdio.h>
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
char arr[64] = "hi, disen!";
|
||
|
// 实现 strlen() 的功能, 统计字符串的长度
|
||
|
int i = 0;
|
||
|
while (arr[i])
|
||
|
i++;
|
||
|
printf("%s length: %d\n", arr, i);
|
||
|
|
||
|
// 反转字符串
|
||
|
int j = 0;
|
||
|
int mid = i >> 1; // 含义: i / 2, 位运算更快
|
||
|
while (j < mid)
|
||
|
{
|
||
|
arr[j] ^= arr[i - j - 1];
|
||
|
arr[i - j - 1] ^= arr[j];
|
||
|
arr[j] ^= arr[i - j - 1];
|
||
|
j++;
|
||
|
}
|
||
|
printf("%s\n", arr);
|
||
|
}
|