C++基础-反转字符串
程序员文章站
2022-07-14 08:05:06
...
反转字符串
void reverseStr(char *m_str)
{
char *p1 = m_str;
char *p2 = m_str + strlen(m_str)-1;
while (p1<p2)
{
char c = *p2;
*p2 = *p1;
*p1 = c;
p1++;
p2--;
}
}
void main()
{
char str[] = "abcdefghijk";
printf("反转之前:%s\n", str);
reverseStr(str);
printf("反转之后:%s\n", str);
reverseStr(str);
printf("反转回来:%s\n", str);
system("pause");
}
结果:
反转思想: