【面試準備】字符串反序

#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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章