用C语言实现字符串反转函数strrev的经典方法

用C语言实现字符串反转函数strrev的经典方法

分类: 玄之又玄代码空间 6621人阅读 评论(7) 收藏 举报

字符串反转函数strrev不是C语言标准库函数,很多C语言编译器并没有提供对它的支持,比如你在Linux下输入Shell命令man 3 strlen,屏幕上会显示,

[plain] view plain copy
  1. STRLEN(3)                  Linux Programmer's Manual                 STRLEN(3)  
  2.   
  3.   
  4.   
  5. NAME  
  6.        strlen - calculate the length of a string  
  7.   
  8. SYNOPSIS  
  9.        #include <string.h>  
  10.   
  11.        size_t strlen(const char *s);  
  12.   
  13. DESCRIPTION  
  14.        The  strlen() function calculates the length of the string s, excluding  
  15.        the terminating null byte ('\0').  
  16.   
  17. RETURN VALUE  
  18.        The strlen() function returns the number of characters in s.  
  19.   
  20. CONFORMING TO  
  21.        SVr4, 4.3BSD, C89, C99.  
  22.   
  23. SEE ALSO  
  24.        string(3), strnlen(3), wcslen(3), wcsnlen(3)  
  25.   
  26. COLOPHON  
  27.        This page is part of release 3.35 of the Linux  man-pages  project.   A  
  28.        description  of  the project, and information about reporting bugs, can  
  29.        be found at http://man7.org/linux/man-pages/.  
  30.   
  31.   
  32.   
  33. GNU                               2011-09-28                         STRLEN(3)  

告诉你strlen函数所实现的功能是calculate the length of a string,引用时需要#include <string.h>。而如果输入man 3 strrev命令,Shell会告诉你,
[plain] view plain copy
  1. 在第 3 节中没有关于 strrev 的手册页条目。  
通过以上操作,也验证了GCC没有提供对strrev的支持,由于它并属于标准函数,编译器有理由不支持的。


strrev函数不常用,不过在进行数制转换和加密等场合还是有机会用到,因为一些针对嵌入式平台的编译器和VC对它提供了支持。对于不支持strrev函数的编译器,许多网友提供了不少很有价值的解决方案,不过直接从VC所提供的源代码中去提炼,似乎更能够兼顾效率和移植性,以下提供一份经典的实现代码:

  1. char* strrev(char* s)  
  2. {  
  3.     /* h指向s的头部 */  
  4.     char* h = s;      
  5.     char* t = s;  
  6.     char ch;  
  7.   
  8.     /* t指向s的尾部 */  
  9.     while(*t++){};  
  10.     t--;    /* 与t++抵消 */  
  11.     t--;    /* 回跳过结束符'\0' */  
  12.   
  13.     /* 当h和t未重合时,交换它们所指向的字符 */  
  14.     while(h < t)  
  15.     {  
  16.         ch = *h;  
  17.         *h++ = *t;    /* h向尾部移动 */  
  18.         *t-- = ch;    /* t向头部移动 */  
  19.     }  
  20.   
  21.     return(s);  

===================================================================================

C++ STL中string的翻转函数名是什么(相当于C中的strrev(s))?


好像没有,我一般是使用通用算法中的reverse
std:;reverse(mystring.begin(), mystring.end());


template <class BidirectionalIterator>
void reverse(BidirectionalIterator first, BidirectionalIterator last);

Reverse reverses a range. That is: for every i such that 0 <= i <= (last - first) / 2), it exchanges *(first + i) and *(last - (i + 1)). 

Defined in the standard header algorithm


#include <iostream>
#include <string>
using namespace std;
void main()
{
string s="123456";
reverse(s.begin(),s.end());
cout<<s;
}
//为什么会在运行时出错?


ADD:
#include <algorithm>

还是出错!

用上了system(...),请再加上#include <cstdlib>

=======================================================================================

反转字符串(c++实现)

发表于3年前(2012-06-07 16:52)   阅读( 608) | 评论( 01人收藏此文章,我要收藏
赞0

大约十一点零八发,秒杀云主机赢P8手机


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
void reverse( char *str, int len) //反转函数,len指要反转字符串的长度
{
  char *p=str,*q=str+len-1,temp;  //指针加法的单位是指针对应类型的字节数,此处因为是char,所以即为1
  while (*q== '\0' )
      q--;
  while (q>p)
    {
      temp=*p;
      *p=*q;
      *q=temp;
  
      p++;
      q--;
    }
}
  
int main()
{
    char a=53;
    cout<<a<<endl; //输出的为ascii码53位所代表的字符,即5
    char str[11]= "ackd12121" //从第四位至第十位全为空(\0),即字符串结束标志
    cout<<str[5]; //输出为空
    cout<< sizeof (str)<<endl;
    reverse(str, sizeof (str)-1);  //sizeof为长度
    cout<<str<<endl;
    //   cout<<'\n'<<endl;   //反斜杠n为回车(换行) endl也为换行,并刷新缓冲区
    cout<< "换行结束" <<endl;
    system ( "pause" );
    return 0;
}

http://www.cnblogs.com/pianoid/archive/2011/10/30/string_reverse_in_c_language.html
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值