1. 程式人生 > >CodeForces-118A——Vjudge String Task

CodeForces-118A——Vjudge String Task

String Task CodeForces

- 118A:

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.

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:

Input
tour
Output
.t.r

Input
Codeforces
Output
.c.d.f.r.c.s

Input
aBAcAba
Output
.b.c.b

問題簡述:輸入一串僅有英文字母的連續的字串,把大寫變小寫,刪去每個母音aoyeui,在每個子音前加點‘.’。
程式1說明:獲取字串後,將字元一個個輸入,每輸入一個字元過一遍迴圈,字元若大寫,則改為小寫,再刪除母音,後輸出“.%c”,避免了繁瑣的陣列運算,記憶體佔用很低。

AC通過的程式碼如下(基於c語言):

#include<stdio.h>

int main()
{
	char a;
	for(scanf("%c",&a);a!='\n';scanf("%c",&a))
	{
		if(a>=65 && a<=90) a+=32;
		if(a!='a'&&a!='o'&&a!='y'&&a!='e'&&a!='u'&&a!='i')
		   {
		   		printf(".%c",a);
		   }
	}
}

未通過的程式碼(Wrong answer on test 34 錯誤原因未知)如下

#include<stdio.h>

int main()
{
	int i,j;
	char str[100]={0};
	char str2[200]={0};
	scanf("%s",str);
	for(i=0;str[i]!=NULL;i++)
	{
		if(str[i]=='A'||str[i]=='a'||str[i]=='O'||str[i]=='o'||\
		   str[i]=='Y'||str[i]=='y'||str[i]=='E'||str[i]=='e'||\
		   str[i]=='U'||str[i]=='u'||str[i]=='I'||str[i]=='i')
		{
			for(j=i;str[j]!=NULL;j++)
			{
				str[j]=str[j+1];
			}
			i--;
			str[j]=NULL;
		}
		if(str[i]>=65 && str[i]<=90)
		str[i]+=32;
	}
	for(i=0;str[i]!=NULL;i++)
	{
		str2[2*i+1]=str[i];
		str2[2*i]='.';
	}
	if(str2[0]=='\0')
	{
		return 0;
	}
	for(i=0;str2[i]!=NULL;i++)
	{
		printf("%c",str2[i]);
	}
	return 0;
}