//加了下面两个头文件,是为了在Win32工程中使用MFC的特性!
#include <afx.h>
#include <afxwin.h>
#include "stdio.h"
#include "conio.h"
////加了下面两句,是为了能够用string(basic_string类型)
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
// string _strtemp = "1111";
// TRACE("/n string = %s /n",_strtemp.c_str() );
// printf("/n string = %s /n",_strtemp.c_str() );
// printf("/n Hello World! /n");
// getch();
CString sTemp;
sTemp.Format("jkgja");
AfxMessageBox(sTemp);
return 0;
}
/************************************************************************/
/* 实现字符串逆转 */
/************************************************************************/
/////方案一 用纯C函数实现
/*#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "abcdefghijklmn";
cout<<str.c_str()<<endl;
cout<<strrev(strdup(str.c_str()))<<endl;
system("pause");
return 0;
}*/
///// 方案二 用STL函数实现
// reverse_copy.cpp
// compile with: /EHsc
// Illustrates how to use the reverse_copy function.
//
// Functions:
// reverse_copy - Reverse a sequence, copy the results to another
// same-sized sequence.
//
//////////////////////////////////////////////////////////////////////
/*
// disable warning C4786: symbol greater than 255 character,
// okay to ignore
#pragma warning(disable: 4786)
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std ;
int main()
{
const int VECTOR_SIZE = 8 ;
// Define a template class vector of strings
typedef vector<string> StrVector ;
//Define an iterator for template class vector of strings
typedef StrVector::iterator StrVectorIt ;
StrVector Tongue_Twister(VECTOR_SIZE) ;
StrVector Reversed_Twister(VECTOR_SIZE) ;
StrVectorIt start, end, it, RTstart, RTend ;
start = Tongue_Twister.begin() ; // location of first
// element of Tongue_Twister
end = Tongue_Twister.end() ; // one past the location last
// element of Tongue_Twister
RTstart = Reversed_Twister.begin() ; // location of first
// element of Reversed_Twister
RTend = Reversed_Twister.end() ; // one past the location last
// element of Reversed_Twister
//Initialize vector Tongue_Twister
Tongue_Twister[0] = "she";
Tongue_Twister[1] = "sells";
Tongue_Twister[2] = "sea";
Tongue_Twister[3] = "shells";
Tongue_Twister[4] = "by";
Tongue_Twister[5] = "the";
Tongue_Twister[6] = "sea";
Tongue_Twister[7] = "shore";
cout << "Before calling reverse_copy" << endl ;
// print content of Tongue_Twister
cout << "Try this Tongue Twister:";
for(it = start; it != end; it++)
cout << " " << *it;
// reverse the items in the vector Tongue_Twister
// and copy the results to Reversed_Twister
reverse_copy(start, end, RTstart);
cout << "/n/nAfter calling reverse_copy" << endl ;
// print content of Tongue_Twister
cout << "Tongue_Twister: ";
for(it = start; it != end; it++)
cout << *it << " ";
// print content of Reversed_Twister
cout << "/n/nNow try the reversed Tongue Twister:" ;
for(it = RTstart; it != RTend; it++)
cout << " " << *it;
system("pause");
return 0;
} */
/*#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(void)
{
string str = "abcdefghijklmn";
reverse_copy(str.begin(),str.end(),ostream_iterator<char>(cout,""));
cout<<endl;
system("pause");
return 0;
}*/
///// 方案三 用string::iterator,前面一个,屁股一个,相互交换,中间靠拢,直到相遇/交叉
/*#include<iostream>
#include<string>
using namespace std;
int main()
{
string str="abcdefghijklmn";
string::reverse_iterator r_iter;//用反响迭代器操作
for(r_iter=str.rbegin();r_iter!=str.rend();++r_iter)
{
cout<<*r_iter;
}
cout<<endl;
system("pause");
return 0;
}
*/
/*#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
string str = "abcdefghijklmn";
copy(str.rbegin(),str.rend(),ostream_iterator <char>(cout,""));
cout << endl;
system("pause");
return 0;
} */
////// 方案四 自己用C实现一个函数reverse_str
/*#include <iostream>
#include <string>
using namespace std;
void reverse_str(unsigned char *str)
{
unsigned char *p;
p = str;
while(*p != '/0')
{
p++;
}
--p;
while(str < p)
{
*p = *str + *p;
*str = *p - *str;
*p = *p -*str;
++str;
--p;
}
}
int main(void)
{
unsigned char str[]="abcdefghijklmn";
cout<<str<<endl;
reverse_str(str);
cout<<str<<endl;
system("pause");
return 0;
} */
/*#include <iostream>
#include <string>
using namespace std;
int main(void)
{
char *str = "abcdefghijklmn";
cout<< str <<endl;
char xx[20] = {0};
int i=0, j=0;
for( i=strlen(str)-1; i>=0;)
{
xx[j++]=str[i--];
}
xx[j]='/0';
cout<<xx<<endl;
system("pause");
return 0;
}
*/
/////////方案五 重写C的函数
char* strrev( char *string )
strrev函数说明: 修改了stirng本身的值!
strrev函数用法如下
int main(void)
{
char aa[] = "123456789";
cout<<"aa = "<<aa<<endl;
cout<<endl;
char *bb = NULL;
bb = strrev(aa);
cout<<"aa = "<<aa<<endl;
cout<<"bb = "<<bb<<endl;
getch();
return 0;
}
**********下面是我们自己重写这个函数 取名为char* __strrev( char *string )
//C的字符串逆转函数
char* __strrev( char *string )
{
char ch = NULL;
int len = strlen( string );
for ( int i=0;i<len/2;i++ )
{
ch = *(string+i);
*(string+i) = *(string+len-1-i);
*(string+len-1-i) = ch;
}
return string;
}
int main(void)
{
char aa[] = "abcdefg";
cout<<"aa = "<<aa<<endl<<endl;
char *bb = NULL;
bb = __strrev(aa);
cout<<"aa = "<<aa<<endl;
cout<<"bb = "<<bb<<endl;
getch();
return 0;
}