1.7编程基础之字符串

统计数字字符个数

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    
    
	char ch;
	int cnt = 0;
	
	while (cin >> ch) {
    
    
		if (ch >= '0' && ch <= '9') cnt ++;
	}
	cout << cnt;

	return 0;
}

找第一个只出现一次的字符

#include <iostream>
#include <cstdio>
using namespace std;

string s;
int cnt[26];

int main() {
    
    
	cin >> s;
	
	for (int i = 0; i < s.size(); i ++) {
    
    
		cnt[s[i]-'a'] ++;
	}
	
	for (int i = 0; i < s.size(); i ++) {
    
    
		if (cnt[s[i]-'a'] == 1) {
    
    
			cout << s[i];
			return 0;
		}
	}

	cout << "no";
	
	return 0;
}

基因相关性

#include <iostream>
#include <cstdio>
using namespace std;

double val;
string a;
string b;

int main() {
    
    
	cin >> val >> a >> b;
	
	int cnt = 0;
	for (int i = 0; i < a.size(); i ++) {
    
    
		if (a[i] == b[i]) cnt ++;
	}
	
	if (1.0 * cnt / a.size() >= val) cout << "yes";
	else cout << "no";
	
	return 0;
}

石头剪子布

#include <iostream>
#include <cstdio>
#include <unordered_map>
using namespace std;

int n;
unordered_map<string, int> m;

int main() {
    
    
	m["Rock"] = 2;
	m["Scissors"] = 1;
	m["Paper"] = 0;
	
	cin >> n;
	
	for (int i = 1; i <= n; i ++) {
    
    
		string p1, p2;
		cin >> p1 >> p2;
		
		int res = (m[p1] - m[p2] + 3) % 3;
		if (res == 1) puts("Player1");
		if (res == 0) puts("Tie");
		if (res == 2) puts("Player2");
	}
	
	return 0;
}

输出亲朋字符串

#include <iostream>
#include <cstdio>
using namespace std;

string s;

int main() {
    
    
	getline(cin, s);
	int len = s.size();
	
	for (int i = 0; i < len; i ++) {
    
    
		cout << char(s[i] + s[(i+1)%len]);
	}
	
	return 0;
}

合法 C 标识符

#include <iostream>
#include <cstdio>
using namespace std;

string s;

int main() {
    
    
	cin >> s;
	
	if (s[0] >= '0' && s[0] <= '9') {
    
    
		puts("no");
		return 0;
	}
	
	for (int i = 0; i < s.size(); i ++)	 {
    
    
		if (s[i] >= '0' && s[i] <= '9') continue;
		else if (s[i] >= 'A' && s[i] <= 'Z') continue;
		else if (s[i] >= 'a' && s[i] <= 'z') continue;
		else if (s[i] == '_') continue;
		else {
    
    
			puts("no");
			return 0;
		}
	}
	
	puts("yes");
	
	return 0;
}

配对碱基链

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    
    
	char ch;
	
	while (cin >> ch) {
    
    
		if (ch == 'A') cout << 'T';
		if (ch == 'T') cout << 'A';
		if (ch == 'G') cout << 'C';
		if (ch == 'C') cout << 'G';
	}	
	
	return 0;
}

字符替换

#include <iostream>
#include <cstdio>
using namespace std;

string s;
char a, b;

int main() {
    
    
	cin >> s >> a >> b;
	
	for (int i = 0; i < s.size(); i ++) {
    
    
		if (s[i] == a) cout << b;
		else cout << s[i];
	}
	
	return 0;
}

密码翻译

#include <iostream>
#include <cstdio>
using namespace std;

string s;

int main() {
    
    
	getline(cin, s);
	
	for (int i = 0; i < s.size(); i ++) {
    
    
		char ch = s[i];
		
		if (ch >= 'a' && ch <= 'z') {
    
    
			printf("%c", (ch-'a'+1)%26+'a');
		}
		else if (ch >= 'A' && ch <= 'Z') {
    
    
			printf("%c", (ch-'A'+1)%26+'A');
		}
		else {
    
    
			printf("%c", ch);
		}
	}
	
	return 0;
}

简单密码

#include <iostream>
#include <cstdio>
using namespace std;

string s;

int main() {
    
    
	getline(cin, s);
	
	for (int i = 0; i < s.size(); i ++) {
    
    
		char ch = s[i];

		if (ch >= 'A' && ch <= 'Z') {
    
    
			printf("%c", (ch-'A'+26-5)%26+'A');
		}
		else {
    
    
			printf("%c", ch);
		}
	}
	
	return 0;
}

潜伏者

#include <iostream>
#include <cstdio>
using namespace std;

string s1, s2, s3;
int a[26][26], st1[26], st2[26];

int main() {
    
    
	cin >> s1 >> s2;

	for (int i = 0; i < s2.size(); i ++) {
    
    
		int y = s1[i] - 'A';	//密文 
		int x = s2[i] - 'A';	//原文 
		st2[x] = 1;
		
		if (a[x][y] == 1) continue;
		else {
    
    
			if (st1[y] == 0) {
    
    
				a[x][y] = 1;
				st1[y] = 1;
			}
			else {
    
    
				puts("Failed");
				return 0;
			}
		}
	}
	
	for (int i = 0; i < 26; i ++) {
    
    
		if (!st2[i]) {
    
    
			puts("Failed");
			return 0;
		}
	}

	cin >> s3;
	for (int i = 0; i < s3.size(); i ++) {
    
    
		int y = s3[i] - 'A';
		for (int x = 0; x < 26; x ++) {
    
    
			if (a[x][y]) printf("%c", x + 'A');
		}
	}
	
	return 0;
}

加密的病历单

#include <iostream>
#include <cstdio>
using namespace std;

string s;

int main() {
    
    
	cin >> s;
	
	for (int i = s.size()-1; i >= 0; i --) {
    
    
		char ch = s[i];
		
		if (ch < 'a') {
    
    
			printf("%c", (ch-'A'+3)%26 + 'a');
		}
		else {
    
    
			printf("%c", (ch-'a'+3)%26 + 'A');
		}
	}
	
	return 0;
}

将字符串中的小写字母转换成大写字母

#include <iostream>
#include <cstdio>
using namespace std;

string s;

int main() {
    
    
	getline(cin, s);
	
	for (int i = 0; i < s.size(); i ++) {
    
    
		if (s[i] >= 'a' && s[i] <= 'z') printf("%c", s[i] - 32);
		else printf("%c", s[i]);
	} 
	
	return 0;
}

大小写字母互换

#include <iostream>
#include <cstdio>
using namespace std;

string s;

int main() {
    
    
	getline(cin, s);
	
	for (int i = 0; i < s.size(); i ++) {
    
    
		if (s[i] >= 'a' && s[i] <= 'z') printf("%c", s[i] - 32);
		else if (s[i] >= 'A' && s[i] <= 'Z') printf("%c", s[i] + 32);
		else printf("%c", s[i]);
	} 
	
	return 0;
}

整理药名

#include <iostream>
#include <cstdio>
using namespace std;

int n;
string s;

int main() {
    
    
	cin >> n;
	
	while (n --) {
    
    
		cin >> s;
		
		if (s[0] >= 'a' && s[0] <= 'z') s[0] -= 32;
		for (int i = 1; i < s.size(); i ++) {
    
    
			if (s[i] >= 'A' && s[i] <= 'Z') s[i] += 32;
		}
		
		cout << s << endl;
	}	
	
	return 0;
}

忽略大小写的字符串比较

#include <iostream>
#include <cstdio>
using namespace std;

string s1, s2;

string init() {
    
    
	string s;
	getline(cin, s);
	
	for (int i = 0; i < s.size(); i ++) {
    
    
		if (s[i] >= 'a' && s[i] <= 'z') s[i] -= 32;
	}
	
	return s;
}

int main() {
    
    
	s1 = init();
	s2 = init();

	if (s1 <  s2) puts("<");
	if (s1 == s2) puts("=");
	if (s1 >  s2) puts(">");
	
	return 0;
}

字符串判等

#include <iostream>
#include <cstdio>
using namespace std;

string s1, s2;

string init() {
    
    
	string s, ans;
	getline(cin, s);
	
	for (int i = 0; i < s.size(); i ++) {
    
    
		if (s[i] >= 'a' && s[i] <= 'z') ans += s[i] - 32;
		if (s[i] >= 'A' && s[i] <= 'Z') ans += s[i];
	}
	
	return ans;
}

int main() {
    
    
	s1 = init();
	s2 = init();

	if (s1 == s2) puts("YES");
	else puts("NO");
	
	return 0;
}
验证子串
#include <iostream>
#include <cstdio>
using namespace std;

string s1, s2;

int main() {
    
    
	cin >> s1 >> s2;
	
	if (s2.find(s1) != -1) cout << s1 << " is substring of " << s2;
	else if (s1.find(s2) != -1) cout << s2 << " is substring of " << s1;
	else cout << "No substring";

	return 0;
}

字符串移位包含问题

#include <iostream>
#include <cstdio>
using namespace std;

string s1, s2;

int main() {
    
    
	cin >> s1 >> s2;
	string a;
	
	if (s1.size() < s2.size()) a = s1, s1 = s2, s2 = a;

	for (int i = 0; i < s1.size(); i ++) {
    
    
		s1 = s1.substr(1) + s1[0];
 
 		if (s1.find(s2) != -1) {
    
    
			puts("true");
			return 0;
		}
	}
	
	puts("false");

	return 0;
}

删除单词后缀

#include <iostream>
#include <cstdio>
using namespace std;

string s;

int main() {
    
    
	cin >> s;
	int len = s.size();

	if (len < 3) cout << s;
	else if (s.substr(len-2) == "er") cout << s.substr(0, len-2);
	else if (s.substr(len-2) == "ly") cout << s.substr(0, len-2);
	else if (s.substr(len-3) == "ing") cout << s.substr(0, len-3);
	else cout << s;
	
	return 0;
}

单词替换

#include <iostream>
#include <cstdio>
using namespace std;

string word[105], s;

int main() {
    
    
	int i = 0;
	while (cin >> s) {
    
    
		word[++i] = s;
	}
	
	for (int j = 1; j <= i - 2; j ++) {
    
    
		if (word[j] == word[i-1]) cout << word[i] << ' ';
		else cout << word[j] << ' ';
	}
		
	return 0;
}

紧急措施

#include <iostream>
#include <cstdio>
using namespace std;

int n;
string email, a, b, c;

int main() {
    
    
	cin >> email >> n;
	
	bool flag = true;
	while (n --) {
    
    
		cin >> a >> b >> c;
		if (c == email)	{
    
    
			flag = false;
			for (int i = 0; i < b.size(); i ++) {
    
    
				if (b[i]>='a' && b[i]<='z') b[i] -= 32;
				else if (b[i]>='A' && b[i]<='Z') b[i] += 32;
			}
			cout << a << ' ' << b << endl;
		}
	}
	
	if (flag) puts("empty");
		
	return 0;
}

过滤多余的空格

#include <iostream>
#include <cstdio>
using namespace std;

string s;

int main() {
    
    
	while (cin >> s) {
    
    
		cout << s << ' ';
	}
		
	return 0;
}

单词的长度

#include <iostream>
#include <cstdio>
using namespace std;

string s;

int main() {
    
    
	cin >> s;
	cout << s.size();
	
	while (cin >> s) {
    
    
		cout << ',' << s.size();
	}
		
	return 0;
}

最长最短单词

#include <iostream>
#include <cstdio>
using namespace std;

string s, a, b;

int main() {
    
    
	cin >> s;
	a = s, b = s;
	
	while (cin >> s) {
    
    
		if (s.size() > a.size()) a = s;
		if (s.size() < b.size()) b = s;
	}
	
	cout << a << endl;
	cout << b << endl;
		
	return 0;
}

字符串最大跨距

#include <iostream>
#include <cstdio>
using namespace std;

string s, a, b;

int main() {
    
    
	char ch;
	while (cin >> ch) {
    
    
		if (ch == ',') break;
		s += ch;
	}
	
	while (cin >> ch) {
    
    
		if (ch == ',') break;
		a += ch;
	}
	
	while (cin >> ch) {
    
    
		if (ch == ',') break;
		b += ch;
	}

	int t = s.rfind(b) - s.find(a) - a.size();
	
	if (t >= 0) cout << t;
	else cout << -1;
		
	return 0;
}

单词翻转

#include <iostream>
using namespace std;

int main() {
    
    
	string s;
	
	getline(cin, s);
	
	for (int i = 0; i < s.size(); i ++ ) {
    
    
		int j = i;
		while (j < s.size() && s[j] != ' ') j ++ ;
		
		for (int k = j - 1; k >= i; k -- ) cout << s[k];
		cout << ' ';
		
		i = j;
	}
	
	return 0;
}

单词倒排

#include <iostream>
#include <cstdio>
using namespace std;

string word[105], s;

int main() {
    
    
	int i = 0;
	while (cin >> s) {
    
    
		word[++i] = s;
	}
	
	for (int j = i; j >= 1; j --) {
    
    
		cout << word[j] << ' ';
	}
		
	return 0;
}

ISBN号码

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    
    
	int ans = 0;
	string s;
	char ch;
	
	for (int i = 1; i <= 9; i ++) {
    
    
		cin >> ch;
		if (ch == '-') {
    
    
			s += '-';
			cin >> ch;
		}
		
		s += ch;
		ans += (ch - '0') * i;
	}
	char id = ans%11==10 ? 'X' : ans%11+'0';
	
	cin >> ch >> ch;
	
	if (id == ch) puts("Right");
	else cout << s << '-'  << id;

	return 0;
}

字符环


猜你喜欢

转载自blog.csdn.net/davidliule/article/details/112495269