字符串反转,如将字符串 ABC反转为 CBA

#include 


int reverse(char* s)
{
    // 获取字符串长度
    int len = 0;
    char* p = s;
    while (*p != 0)
    {
        len++;
        p++;
    }
    
    // 交换 ...
    int i = 0;
    char c;
    while (i <= len / 2 - 1)
    {
        c = *(s + i);
        *(s + i) = *(s + len - 1 - i);
        *(s + len - 1 - i) = c;
        i++;
    }return;
}

int main()
{
    char s[] = "abcd";
    printf("'%s' =>\n", s);
    reverse(s);           // 反转字符串
    printf("'%s'\n", s);
    return 0;
}

你可能感兴趣的:(个人)