#include #include #include #include using namespace std; void strReverse(std::string& str) // 反转字符串 包装 reverse() 可以inline { reverse(str.begin(),str.end()); } void cstrReverse(char* cstr) // 反转 C风格字符串 { std::string str(cstr); reverse(str.begin(),str.end()); strcpy(cstr,str.c_str()); } void cstrSort(char* cstr) // 排序 C风格字符串 { std::string str(cstr); sort(str.begin(),str.end()); strcpy(cstr,str.c_str()); } void main() { char cstr[] = "123456788da88afdfd88a889"; cout << cstr << endl; cstrReverse(cstr); cout << cstr << endl; cstrSort(cstr); cout << cstr << endl; }
char result[50]; return result; 你返回了一个局部变量的指针。
你这是C不是C++啊。 运行环境是VC,DEBUG编译选项,返回一个堆栈上的局部变量指针,属于很常见的错误。 你用char * result = new (50*sizeof(char))试试。 或者在转换完成后,strcpy(str,result); return str;
#include #include using namespace std; int main( ) { string str; string tarstr; cout<<"input your string \n"; cin>>str; int length=str.length( ); int j; for(j=length-1;j>=0;j-- ) { tarstr+=str[j]; } tarstr[ length ]='\0'; cout< return 0; }
给大家一个很简单的例子: C/C++ code #include #include using namespace std; int main( ) { string str; string tarstr; cout<<"input your string \n"; cin>>……
#include #include using namespace std; int main( ) { string str; string tarstr; cout<<"input your string \n"; cin>>str; int length=str.length( ); int j; for(j=length-1;j>=0;j-- ) { tarstr[ length-j-1 ]=str[j]; //注意这里!!!! } tarstr[ length ]='\0'; cout< return 0; }
#include #define max 100 void reverse(char *p); void main() { char s[100]; gets(s); reverse(s); puts(s); } void reverse(char *p) { char *q; char s; q=p; while(*q!='\0') { q++; } q--; while(p { s=*p; *p=*q; *q=s; p++; q--; }
{ s=*p; *p=*q; *q=s; p++; q--; }
引用 10 楼 pcliuguangtao 的回复: 给大家一个很简单的例子: C/C++ code #include #include using namespace std; int main( ) { string str; string tarstr; cout<<"input your string \n"; cin>……
引用 10 楼 pcliuguangtao 的回复: 给大家一个很简单的例子: C/C++ code #include #include using namespace std; int main( ) { string str; string tarstr; cout<<"input your string \n"; ci……