首页 > 代码库 > 翻转字符串

翻转字符串

代码(C++):

class Solution {
public:
/**
* @param s : A string
* @return : A string
*/
string reverseWords(string s) {
// write your code here
stringstream in;
stringstream out;
in << s;
char str[101][101];
char tempStr[1001];
in.getline(tempStr, 1001);
int num = 0;
char *t = NULL;
for (t = strtok(tempStr, " "); t != NULL; t = strtok(NULL, " ")) {
strcpy(str[num++], t);
}
for (int i = num - 1; i >= 0; i--) {
out << str[i] << " ";
}
return out.str();
}
};

技术分享

翻转字符串