美文网首页
C 字符串反转

C 字符串反转

作者: CaptainRoy | 来源:发表于2019-03-21 19:38 被阅读0次
  • 指针反转
void indicator_resver(char *cha)
{
    // 指向第一个字符
    char *begin = cha;
    // 指向最后一个字符
    char *end = cha + (strlen(cha) - 1);
    
    while (begin < end) {
        char temp = *begin;
        *begin = *end;
        *end = temp;
        ++begin;
        --end;
    }
}
char greet[] = "Hello,World";
indicator_resver(greet);
  • 数组字符串反转
void array_resver(char *cha)
{
    char *s = cha;
    NSInteger length = strlen(s);
    for (NSInteger i = 0 , j = length - 1; i < j; i++ ,j--) {
        char temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }
}
char greet[] = "Hello,World";
array_resver(greet);

相关文章

网友评论

      本文标题:C 字符串反转

      本文链接:https://www.haomeiwen.com/subject/dythvqtx.html