String Task解决方案【CodeForces - 118A 】

String Task解决分析

谨以此文章纪念第一次ACM训练…

题目

Petya started to attend programming lessons. On the first lesson his task was to write a simple program. The program was supposed to do the following: in the given string, consisting if uppercase and lowercase Latin letters, it:

  • deletes all the vowels,
  • inserts a character “.” before each consonant,
  • replaces all uppercase consonants with corresponding lowercase ones.

Vowels are letters “A”, “O”, “Y”, “E”, “U”, “I”, and the rest are consonants. The program’s input is exactly one string, it should return the output as a single string, resulting after the program’s processing the initial string.
Help Petya cope with this easy task.

Time limitMemory limitSourceCodeforcesTagEditorial
2000 ms262144 kBBeta Round #89 (Div. 2)simplementation strings *1100Announcement Tutorial #1 Tutorial #2

Input

The first line represents input string of Petya’s program. This string only consists of uppercase and lowercase Latin letters and its length is from 1 to 100, inclusive.

Output

Print the resulting string. It is guaranteed that this string is not empty.

Example

inputoutput
tour.t.r
Codeforces.c.d.f.r.c.s
aBAcAba.b.c.b
问题链接:CodeForces - 118A

问题描述

输入一串含有大写和小写的字母,将其中的元音字母1删除,并在每一个辅音字母前加上一个点(.),最后将所有的大写字母换成小写字母。

问题分析

首先获得用户的输入存于字符串变量中,然后找出其中的元音字母并将其删除。之后再在剩余的字母前面统一加点,最后将大写换成小写。

AC通过的C++语言程序代码如下:

#include<string>
#include <iostream>
using namespace std;
int main()
{
	string str;
	cin >> str;
	for (int i = 0; i < str.size(); i++)
		if (str[i] == 'A' || str[i] == 'O' || str[i] == 'Y' || str[i] == 'E' || str[i] == 'U' || str[i] == 'I' || str[i] == 'a' || str[i] == 'o' || str[i] == 'y' || str[i] == 'e' || str[i] == 'u' || str[i] == 'i')
		{
			str = str.erase(i, 1);
			i--;
		}
	for (int i = 0; i < str.size(); i += 2)
		str.insert(i, ".");
	for (int n = 0; n < str.size(); n++)
		if (str[n] >= 65 && str[n] <= 90)str[n] += 32;
	cout << str << endl;
	return 0;
}

代码分析

此程序使用到了string类,所以要包括头文件string。

首先是删除元音字母:

//删除字符串数组中的元音字母
for (int i = 0; i < str.size(); i++)
		if (str[i] == 'A' || str[i] == 'O' || str[i] == 'Y' || str[i] == 'E' || str[i] == 'U' || str[i] == 'I' || str[i] == 'a' || str[i] == 'o' || str[i] == 'y' || str[i] == 'e' || str[i] == 'u' || str[i] == 'i')
		{
			str = str.erase(i, 1);
			i--;
		}

这里需要注意的是当第i个字母被删除之后,由于后面的字母会补上来,第i个字母就会变成后面一个字母,然而如果后面一个字母是元音字母而i直接自增就会导致这个字母被跳过,其后果就是有残留的元音字母,解决办法就是在删除字母之后加一行i- -以防下一个字母被跳过。

之后由于字符串数组中的元音字母已经全部被删除,剩余的必然全都是辅音字母。
于是直接在每个字母前加点:

//在辅音字母前加点
for (int i = 0; i < str.size(); i += 2)
		str.insert(i, ".");

这里要注意的是加了点之后字符串数组元素会整体右移,所以要把i切换成下一个字母就等把i加2,而非加1。

最后将所有大写字母换成小写字母2

//将大写字母换为小写字母
for (int n = 0; n < str.size(); n++)
		if (str[n] >= 65 && str[n] <= 90)str[n] += 32;

然后输出数组就大功告成啦!


  1. 元音字母为:“A”, “O”, “Y”, “E”, “U”, “I”,剩余的字母全是辅音字母 ↩︎

  2. 大写字母与小写字母的ASCII码值相差32 ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值