字符串加空格
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
string s;
getline (cin, s);
for (int i = 0; i < s.size(); i ++ )
cout << s[i] << " ";
}
字符串插入
#include<bits/stdc++.h>
using namespace std;
string st1,st2;
char x;
int i,p;
int main()
{
while(cin>>st1>>st2){
p=0;
x=0;
for(i=0;i<=st1.size()-1;i++)
if(st1[i]>x){
x=st1[i];
p=i;
}
for(i=0;i<=p;i++)cout<<st1[i];
cout<<st2;
for(i=p+1;i<=st1.size()-1;i++)cout<<st1[i];
cout<<endl;
}
}
作者:垫底抽風
链接:https://www.acwing.com/solution/content/63316/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
只出现一次的字符
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main()
{
string A;
bool s=0;
while(getline(cin,A))
{
for(int i=0;i<A.size();i++)
{
if(A.find(A[i])==A.rfind(A[i]))
{
cout<<A[i]<<endl;
s=1;
break;
}
}
}
if(s==0)
cout<<"no"<<endl;
return 0;
}
忽略大小写比较字符串大小
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string s1, s2;
getline(cin, s1);
getline(cin, s2);
for (int i = 0; s1[i]; i ++ ) if (s1[i] >= 'A' && s1[i] <= 'Z') s1[i] += 32;
for (int i = 0; s2[i]; i ++ ) if (s2[i] >= 'A' && s2[i] <= 'Z') s2[i] += 32;
for (int i = 0; s1[i] || s2[i]; i ++ )
{
if (s1[i] > s2[i])
{
puts(">");
return 0;
} else if (s1[i] < s2[i]) {
puts("<");
return 0;
}
}
puts("=");
return 0;
}
去掉多余的空格
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
string s;
getline(cin,s);
string ans;
for(int i=0;s[i];i++){
if(s[i]!=' '){
ans+=s[i];
}else{
ans+=' ';
int j=i;
while(s[j] && s[j]==' '){
j++;
}
i=j-1;
}
}
cout<<ans<<endl;
return 0;
}