C++实现对字符串s以a为首b为尾的任意子串翻转后输出,并对a和b的合法性加以判断,参考代码如下:
#include
#include
using namespace std;
void reverse(char *str,int len)
{
int i;
char t;
for(i=0; i
str[i]=str[len-i-1];
str[len-i-1]=t;
}
}
int main()
{
char s[100];
int a,b,len;
cout << "输入字符串s:" << endl;
cin>>s;
cout << "输入整数a b:" << endl;
cin>>a>>b;
len=strlen(s);
if(a>b||a<0||b>=len) {
cout << "false" << endl;
return 0;
}
reverse(&s[a],b-a+1);
cout << s << endl;
return 0;
}