例如 将www.google.com.hk 翻转为 hk.com.google.www // Reverse.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> void swapPtr(char* first, char* second) { //交换的是指针所指内存的值,指针并没有交换 char tmp; tmp = *first; *first = *second; *second = tmp; } //将subString开始的len个字符翻转 void reverseSub(char* subString, int len) { for (int i = 0; i < len / 2; ++i) { swapPtr(subString + i, subString + len - 1 - i); } } int length(char* ptr) { int len = 0; while(*(ptr++) != '/0') { ++len; } return len; } int findDot(char* ptr) { char* p = ptr; int len = 0; while(*p != '/0' && *p != '.') { ++len; ++p; } return len; } void reverse(char* ptr) { char* pBegin = ptr; int subLen = 0; while(*pBegin != '/0') { subLen = findDot(pBegin); reverseSub(pBegin, subLen); pBegin = pBegin + subLen + 1; } } int main(int argc, char* argv[]) { char str[] = "http://sports.sina.com.cn/nba"; char* src = str; printf("original: %s/n", src); int len = length(src); printf("len = %d/n", len); reverseSub(src, len); printf("reversed: %s/n", src); reverse(src); printf("after process: %s/n", src); return 0; }