反转一个字符串

来源:互联网 发布:网络项目吧 编辑:程序博客网 时间:2024/05/29 18:44
// strReverse.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <string.h>//、反转一个字符串。void swap(char& a,char& b){char tmp;tmp=a;a=b;b=tmp;}void reverse( char * str){//char tmp;int len;len = strlen(str);for ( int i = 0 ;i < len / 2 ; ++ i){//tmp = char [i];//str[i] = str[len - i - 1 ];//str[len - i - 1 ] = tmp;swap(str[i],str[len - i - 1 ]);}}int main(int argc, char* argv[]){char null[]="";reverse(null);printf("reverse(null):%s\n",null);char a[]="A";reverse(a);printf("reverse(a):%s\n",a);char b[]="AB";reverse(b);printf("reverse(b):%s\n",b);char c[]="ABC";reverse(c);printf("reverse(c):%s\n",c);char d[]="ABCD";reverse(d);printf("reverse(d):%s\n",d);printf("Hello World!\n");return 0;}/*reverse(null):reverse(a):Areverse(b):BAreverse(c):CBAreverse(d):DCBAHello World!Press any key to continue*/

原创粉丝点击