Codeforces Problem-112A Petya and Strings
关于transform函数:
transform函数:对于一段区间内的所有数据进行相同的操作。
函数原型:transform (first, end, result, operation);
字符串转换成小写字母代码:
#include <iostream>
#include <algorithm>
using namespace std;
char op(char ch)
{
if(ch>='A'&&ch<='Z') //大写字母 if (ch>='a'&&ch<='z') return ch-32;
return ch+32;
else
return ch;
}
int main()
{
string first,second;
cin>>first;
second.resize(first.size());
transform(first.begin(),first.end(),second.begin(),op);
cout<<second<<endl;
return 0;
}
AC代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1,s2;
cin>>s1>>s2;
transform(s1.begin(),s1.end(),s1.begin(),::tolower);
//操作区间:s1整条字符串,将结果写回s1中;
//::tolower:将字符串变为小写字符串;::toupper:将字符串变为大写字符串
transform(s2.begin(),s2.end(),s2.begin(),::tolower);
if (s1<s2)
puts("-1");
else if (s1==s2)
puts("0");
else
puts("1");
return 0;
}