字符串长度
# include <iostream>
# include <string>
using namespace std;
const int N = 105 ;
int main ( )
{
string str;
getline ( cin, str) ;
cout << str. size ( ) ;
}
761. 字符串中的数字个数
# include <iostream>
# include <string>
using namespace std;
const int N = 105 ;
int main ( )
{
string str;
getline ( cin, str) ;
int sum= 0 ;
for ( int i = 0 ; i < N; i ++ ) {
if ( str[ i] >= '0' && str[ i] <= '9' ) {
sum++ ;
}
}
cout << sum;
}
763. 循环相克令
# include <iostream>
# include <string>
using namespace std;
int main ( )
{
int t;
cin >> t;
while ( t-- > 0 ) {
string p1, p2;
cin >> p1;
cin >> p2;
int x1= p1. size ( ) ;
int x2= p2. size ( ) ;
int x= x1- x2;
if ( x== 3 || x== - 1 || x== - 2 ) {
cout << "Player1" << endl;
} else if ( x== 0 ) {
cout << "Tie" << endl;
} else {
cout << "Player2" << endl;
}
}
return 0 ;
}
765. 字符串加空格
# include <iostream>
# include <string>
using namespace std;
int main ( )
{
string str;
getline ( cin, str) ;
cout << str[ 0 ] ;
for ( int i = 1 ; i < str. size ( ) ; i ++ ) {
cout << " " ;
cout << str[ i] ;
}
return 0 ;
}
769. 替换字符
# include <iostream>
# include <string>
using namespace std;
int main ( )
{
string str;
char c;
getline ( cin, str) ;
cin >> c;
for ( int i = 0 ; i < str. size ( ) ; i ++ ) {
if ( str[ i] == c) {
cout << "#" ;
} else {
cout << str[ i] ;
} }
return 0 ;
}
773. 字符串插入
# include <iostream>
using namespace std;
int main ( )
{
string s, sub;
while ( cin >> s>> sub)
{
int idx= 0 ;
for ( int i= 0 ; i< s. size ( ) ; i++ )
if ( s[ i] > s[ idx] )
{
idx= i;
}
s. insert ( idx+ 1 , sub) ;
cout<< s<< endl;
}
}
772. 只出现一次的字符
# include <iostream>
# include <cstdio>
# include <string>
using namespace std;
int main ( )
{
string A;
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;
return 0 ;
}
}
}
cout<< "no" << endl;
return 0 ;
}