【面试准备】字符串反序

来源:互联网 发布:淘宝网店书籍推荐 编辑:程序博客网 时间:2024/04/30 21:14
#include <iostream>#include <string>#include <stdlib.h>using namespace std;void reverseWords(string &s) {        string a[s.length()];        int j = 0;        int aa = 0;        int i = 0;        char *w = (char*) malloc(s.length());        for( i = 0 ; (unsigned)i < s.length(); ++i){            if(s.at(i)!=' '){                w[j++] = s.at(i);            }            if(s.at(i)!=' '&&((unsigned)i+1 == s.length()||s.at(i+1) == ' ')){            w[j] = '\0';                j = 0;                a[aa++] = w;            }    }    for(int i = aa-1 ; i >= 0 ;--i){        cout<<a[i]<<" ";    }}int main(){string s = "the sky is blue";reverseWords(s);return 0;}

0 0
原创粉丝点击