C++程序员面试函数 字符窜倒序、空格 (三)

来源:互联网 发布:淘宝如何设置客服 编辑:程序博客网 时间:2024/06/15 03:31

1.写一个函数将"tom is cat" 倒序打印出来,即 "cat is tom"。

#include "stdafx.h"#include <iostream>using namespace std;//反字符串#define SPACE ' '  //或者使用const char SPACE = ' ';int main(){char* str = "Tom is cat fgfgfgfg juytf"; // 字符串char* first= str+strlen(str)-1;char* second = first + 1; // 开始时 俩都指向字符串结尾处while(str != first--)       //指针从结尾处开始循环递减到 字符串为空的{if(SPACE == *first)     //第一个单词{for (int i = 1; first + i != second; ++i){cout << *(first + i); }cout << " ";second = first;}if (str == first){for (int i = 0; first + i != second; ++i){cout << *(first + i);  }cout << endl;}}}
输出juytffgfgfgfgcat is Tom   

2.写一个递归函数将内存中的字符串翻转"abc"->"cba"

3.编写一个函数,把字符串中的所有字符子串的各种组合形式全部都显示出来。字符子串的长度范围是从一个字符到字符串的长度。不管排列顺序如何,只要两种组合中的字符完全一样,他们就是同一种组合。比如给定字符串“hart”,则“ha”和“har”是不同的组合,

而“ha”和“ah”是相同的组合。


原创粉丝点击