C/C++ programming learning-week 9 ⑦ emergency measures

Topic link

Title description

Recently, some popular websites have been hacked, and their accounts, passwords and email data have been leaked. You have registered several accounts on these websites (the usernames used are not necessarily the same), but you used the same email when registering. You got the leaked data at this time and hope to change your password as soon as possible. The strategy is as follows: Find your username and password based on your email, and then change the password. The changed rule is: lowercase and uppercase are swapped, and non-letter characters remain unchanged.

Input format The
first line is your email address, which is no more than 50 characters long and contains only letters, numbers and the'@' symbol.

The second line is the number of accounts N, N (0<N<10000).

The next N lines, each line represents an account, the format is:

Username password email

Use a single space to separate them. The user name, password, and email do not contain spaces, and the length does not exceed 50 characters.

The output format
has several lines, each line for your account, including: your account, the modified password (separated by a single space).

If you don't have your account, output "empty".

Sample Input

abc@pku.edu.cn
5
helloKitty iLoveCats abc@pku.edu.cn
2012 maya2012 cplusplus@exam.com
KittyCat 5iKitty abc@pku.edu.cn
program password teacher@exam.com
whoAmi Feb.29$ abc@pku.edu.cn

Sample Output

helloKitty IlOVEcATS
KittyCat 5IkITTY
whoAmi fEB.29$

Sample Input 2

abc@pku.edu.cn
1
2012 maya2012 cplusplus@exam.com

Sample Output 2

empty

Ideas

First find your account, and then swap lowercase and uppercase, and change the password with the same rules for non-alphabetic characters.

C++ code 1:

#include<iostream>
using namespace std;
string my, user, pass, em;
int n, len, flag;//用flag的值记录是否存在与我们的email一样的账号,0即不存在,1即存在,另外,全局变量flag的初始值为0
int main()
{
    
    
	cin >> my >> n;//读入自己的email和数据总数n
	for(int i = 0; i < n; ++i)
	{
    
    
		cin >> user >> pass >> em;//读入给出的账号密码以及email
		if(em == my)//如果注册的email与我们的一样的话就进行处理,同时更新flag为1
		{
    
    
			flag = 1;
			len = pass.size();//len用来记录密码字符串的长度
			for(int j = 0; j < len;++j)//对密码字符串处理,大小写转换
				if(pass[j] >= 'a' && pass[j] <= 'z') pass[j] -= 32;
				else if(pass[j] >= 'A' && pass[j] <= 'Z') pass[j] += 32;
			cout << user << " " << pass << '\n';//输出处理过的账号密码
		}
	}
	if(flag == 0) cout << "empty" << '\n';//如果不存在与我们的email一样的email,就输出empty
	return 0;
}

C++ code 2:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	string addr, user, key, email;
	while(cin >> addr)
	{
    
    
		int n, flag = 0;
		cin >> n;
		for(int i = 0; i < n; i++)
		{
    
    
			cin >> user >> key >> email;
			if(email == addr)
			{
    
    
				flag = 1;
				cout << user << " ";
				int len = key.length();
				for(int j = 0; j < len; j++)
				{
    
    
					if(key[j] >= 97 && key[j] <= 122) key[j] -= 32;
					else if(key[j] >= 65 && key[j] <= 90) key[j] += 32;
				}
				cout << key << endl;
			}
		}
		if(flag == 0) cout << "empty" << endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113095810