【发布时间】:2011-11-19 17:55:59
【问题描述】:
有没有办法交换字符串中的字符位置?例如,如果我有"03/02",我需要获得"02/03"。
任何帮助表示赞赏!
【问题讨论】:
-
如果您的问题比这两个答案更深,那么您需要扩展它以告诉我们您真正要问的是什么?
有没有办法交换字符串中的字符位置?例如,如果我有"03/02",我需要获得"02/03"。
任何帮助表示赞赏!
【问题讨论】:
当然:
#include <string>
#include <algorithm>
std::string s = "03/02";
std::swap(s[1], s[4]);
【讨论】:
std::swap(str[1], str[4]);
【讨论】:
有。 :)
std::swap(str[i], str[j])
【讨论】:
等等,你真的想要这样一个具体的答案吗?您不在乎字符串是否为 2/3 而不是 02/03?
#include <string.h>
#include <iostream>
bool ReverseString(const char *input)
{
const char *index = strchr(input, (int) '/');
if(index == NULL)
return false;
char *result = new char[strlen(input) + 1];
strcpy(result, index + 1);
strcat(result, "/");
strncat(result, input, index - input);
printf("%s\r\n", result);
delete [] result;
return true;
}
int main(int argc, char **argv)
{
const char *test = "03/02";
ReverseString(test);
}
【讨论】: