单个if{return}
,不要if...else
,挂掉就尽快退出
01 汉字硬反转(天连草色一川平 -> 平川一色草连天)
一些注释都是想过来的思路,就不删除了。
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
char* myReverseString(char* str)
{
if (NULL==str)
{
return;
}
int len = strlen(str);//14
#if 0
printf("%d\n", len);
printf("%s\n", str);
printf("%s\n", str+2);
printf("%s\n", str + 4);
#endif
//#if 0
//0到13,一一反转
for (int i = 0; i < len/2; i++)
{
char temp = str[i];
str[i] = str[len - i-1];
str[len - i - 1] = temp;
}
//13 12...1 0,实际应该是12 13,10 11,...,0 1
for (int i = 0; i < len; i=i+2)
{
char temp = str[i];
str[i] = str[i + 1];
str[i + 1] = temp;
}
//#endif
#if 0
char* p1 = str;
char* p2 = str + len -1;
while (p1 < p2) {
char temp = *p1;
*p1 = *p2;//-52
*p2 = temp;
//p1++;
//p2--;
p1 = p1 + 2;
p2 = p2 - 2;
}
#endif
return str;
}
void test1()
{
char str[] = "天连草色一川平";
//char str[] = "aabbccdd";
//
printf("\n\n%s\n", myReverseString(str));
//printf("%s\n", myReverseString(str));
}
void main() {
test1();
system("pause");
}