这节课学习了string字符串的5个例题,以及两个它的两个常用的函数。
This lesson learned string string five examples, and two of its two commonly used functions.
目录
directory
NO. 1 for the length of the string string functions
NO . 2 substr()函数 #NO%20.%202%20substr()%E5%87%BD%E6%95%B0%C2%A0%C2%A0
NO . 2 substr()函数
例题1:简化单词#%E4%BE%8B%E9%A2%981%EF%BC%9A%E7%AE%80%E5%8C%96%E5%8D%95%E8%AF%8D
Example 1: simplified words
例题2:首字母大写#%E4%BE%8B%E9%A2%982%EF%BC%9A%E9%A6%96%E5%AD%97%E6%AF%8D%E5%A4%A7%E5%86%99
Example 2: capitalize the first letter
例三:男孩或女孩 #%E4%BE%8B%E4%B8%89%EF%BC%9A%E7%94%B7%E5%AD%A9%E6%88%96%E5%A5%B3%E5%AD%A9%C2%A0
Example 3: boy or girl
例四:一个四位数#%E4%BE%8B%E5%9B%9B%EF%BC%9A%E4%B8%80%E4%B8%AA%E5%9B%9B%E4%BD%8D%E6%95%B0
Example 4: a four digits
NO . 1 求string字符串长度的函数
NO. 1 for the length of the string string functions
再求些问题时,我们需要知道字符串的长度,这时我们就要用到他。
Ask some more questions we need to know the length of the string, and then we're going to use him.
使用格式:
Use the format:
字符串名.size();
String name.size();
运用样例:
Using the sample:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string a="abcdefg";
int len=a.size();
cout<<len<<"\n";
return 0;
}
输出:
output :
NO . 2 substr()函数
NO. 2 substr () function
用于截取字符串
Used to capture the string
格式:
Format:
a.substr(3,5)
从a[3]截取到a[5]
From a [3] intercepted a [5]
例题1:简化单词
Example 1: simplified words
题目描述:多测,输入一个数n,表示有几个测试单词,接下来n行,每行一个单词。
Title description: many test, input a number n, said several test words, the next n lines, each line a word.
如果单词长度大于10,输出a[1],字符串长度,a[n-1]。
If the word length is more than 10, output a [1], the length of the string, a [n - 1).
输入:
Input:
5
Title
description
many
test
input
输出:
Output :
Title
d11n
many
test
input
代码:
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
while(n--)
{
string str1;
cin>>str1;
int len=str1.size();
if(len>10)
cout<<str1[len]<<str1[0]<<len-2<<"\n";
else
cout<<str1<<"\n";
}
return 0;
}
例题2:首字母大写
Example 2: capitalize the first letter
题目描述:输入共一行,一个单词,将其单词首字母转换为大写。
Title description: enter a line, a word, the word first letters converted to uppercase.
输入:
Input:
statement
输出:
Output:
Statement
代码:
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
if(s[0]>'Z')
s[0]-='a'-'A';
cout<<s<<"\n";
return 0;
}
例三:男孩或女孩
Example 3: boy or girl
题目描述:有些男孩会用女孩的头像,Timmin为了确定他/她是男孩还是女孩,想出了一个办法。
Title description: some boy with the girl's face, Timmin in order to determine a boy or a girl, is he/she came up with a solution.
这是他的方法:如果一个人的用户名中不同字符的数量是奇数,那么他是男性,否则她是女性。给你一个表示用户名的字符串,请通过他的方法来确定这个用户的性别。
This is his way: if a person is odd number of different characters in user name, so he is a male, or she is female. Give you a user name string, please pass his method to determine the user's gender.
输入
Input:
第一行包含一个非空字符串,只包含小写的英文字母——用户名。字符串长度不超过100个字母。
The first line containing a non-empty string, containing only lowercase English letters - the user name. The length of the string does not exceed 100 characters.
输出
Output:
如果是女性,那就印上“CHAT WITH HER!”否则打印"IGNORE HIM!"。
If you are female, printed on "CHAT WITH HER!" Otherwise, print "IGNORE HIM!" .
代码:
Code:
#include <bits/stdc++.h>
using namespace std;
const int maxc=1000;
bool in[maxc];
int main()
{
string s;
cin>>s;
for(int i=0;i<int(s.size());i++)
{
char c=s[i];
in[int (c)]=true;
}
int dist=0;
for(int i=0;i<maxc;i++)
if(in[i]) dist++;
cout<<(dist%2==1?"IGNORE HIM!":"CHAR WITH HER!");
return 0;
}
例四:一个四位数
Example 4: a four digits
题目描述:
Title description:
给定一个整数N,取值范围为0到9999(含0和9999),在附加必要的零后,将其打印为一个四位数字符串。
Given an integer N, the value range is 0 to 9999 (including 0 and 9999), in addition to the necessary after zero, print it as a string of four digits.
输入:
Input:
321
输出:
Output:
0321
代码:
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a;
cin>>a;
if(a.size()<4)
{
for(int i=0;i<=4-a.size()-1;i++)
cout<<"0";
cout<<a;
}
else
cout<<a;
return 0;
}