[AOJ]Lesson - ITP1 Topic # 8 Character 字符

A: Toggling Cases

Write a program which converts uppercase/lowercase letters to lowercase/uppercase for a given string.

Input

A string is given in a line.

Output

Print the converted string in a line. Note that you do not need to convert any characters other than alphabetical letters.

Constraints

The length of the input string < 1200

Sample Input

fAIR, LATER, OCCASIONALLY CLOUDY.

Sample Output

Fair, later, occasionally cloudy.

题目大意

输入一个字符串,将大写字母变成小写,小写变成大写,并输出变化后的字符串。字符串长度小于1200。

#include <bits/stdc++.h>
using namespace std;
int main(){
	string s;
	getline(cin, s);
	int len = s.size();
	for(int i=0; i<len; i++){
		if(s[i]>='A' && s[i]<='Z')  // 大写变小写
			s[i] += -'A'+'a';
		else if(s[i]>='a' && s[i]<='z') // 小写变大写,注意有else
				s[i] += -'a'+'A';
		cout << s[i];
	}
	cout << endl;
	return 0;
}

B: Sum of Numbers

Write a program which reads an integer and prints sum of its digits.

Input

The input consists of multiple datasets. For each dataset, an integer x is given in a line. The number of digits in x does not exceed 1000.
The input ends with a line including single zero. Your program should not process for this terminal symbol.

Output

For each dataset, print the sum of digits in x.

Sample Input

123
55
1000
0

Sample Output

6
10
1

题目大意

输入若干个数字字符串,求每个字符串中的数字之和。每个字符串长度不超过1000,最后一个字符串以“0”结束。

#include <bits/stdc++.h>
using namespace std;
int main(){
	string s;
	while(cin >> s){
		if(s=="0")
			break;
		int ans = 0;
		for(int i=0; i<s.size(); i++){
			if(s[i]>='0' && s[i]<='9')
				ans += s[i]-'0';
		}
		cout << ans << endl;
	}
	return 0;
}

C: Counting Characters

Write a program which counts and reports the number of each alphabetical letter. Ignore the case of characters.

Input

A sentence in English is given in several lines.

Output

Prints the number of alphabetical letters in the following format:

a : The number of 'a'
b : The number of 'b'
c : The number of 'c'
.
.
z : The number of 'z'

Constraints

The number of characters in the sentence < 1200

Sample Input

This is a pen.

Sample Output

a : 1
b : 0
c : 0
d : 0
e : 1
f : 0
g : 0
h : 1
i : 2
j : 0
k : 0
l : 0
m : 0
n : 1
o : 0
p : 1
q : 0
r : 0
s : 2
t : 1
u : 0
v : 0
w : 0
x : 0
y : 0
z : 0

题目大意

有一个若干行的英文句子,请统计字母a~z的个数。忽略大小写,句子长度不超过1200。

#include <bits/stdc++.h>
using namespace std;
int main(){
	string s;
	int a[26];
	memset(a, 0, sizeof(a));
	while(cin >> s){ // 若干行
		int len = s.size();
		for(int i=0; i<len; i++){
			if(s[i]>='A' && s[i]<='Z') // 统一为小写
				s[i] += 'a'-'A';
			int t = s[i]-'a';
			a[t] ++;  // 统计数量
		}
	}
	for(int i=0; i<26; i++){ // 输出
		cout << (char) (i+'a') << " : " << a[i] << endl;
	}
	return 0;
}

D: Ring

Write a program which finds a pattern p in a ring shaped text s.

Input

In the first line, the text s is given.
In the second line, the pattern p is given.

Output

If p is in s, print Yes in a line, otherwise No.

Constraints

1≤ length of p≤ length of s≤100
s and p consists of lower-case letters

Sample Input 1

vanceknowledgetoad
advance

Sample Output 1

Yes

Sample Input 2

vanceknowledgetoad
advanced

Sample Output 2

No

题目大意

输入两个字符串s和p,第一个字符串s是一个环,问s中是否包含字符串p。如果包含,输出Yes,否则输出No。所有字母都是小写, p和s的长度大于0且小于等于100,并且p不会比s更长。

// 考察环的处理,以及查找函数find()。
#include <bits/stdc++.h>
using namespace std;
int main(){
	string s1, s2;
	cin >> s1 >> s2;
	s1 = s1+s1;  // 环的处理,拼起来 
	if(s1.find(s2, 0)!=s1.npos)  // s1.npos表示在s1中没有位置,即找不到
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
	return 0;
}

posted @ 2019-11-19 14:59  gdgzliu  阅读(1145)  评论(0编辑  收藏  举报