LL Golf Hole 2 - 文字列に含まれる単語の最初の文字を大文字にする
Posted feedbacks - C#
単語と単語の間には空白があるって前提でいいのでしょうか。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using System;
class Program {
static void Main(string[] args) {
Console.WriteLine(ToUpper1st1(args[0]));
Console.WriteLine(ToUpper1st2(args[0]));
Console.ReadLine();
}
//こっちの方が短いけど、' 'が2文字続いたときにstr.Substring(1)が通らない
static string ToUpper1st1(string value) {
string r = "";
foreach(string str in value.Split(new char[] { ' ' })) {
r += str[0].ToString().ToUpper() + str.Substring(1) + " ";
}
return r;
}
//こっちはだいぢょぶ。でも、3項演算子って読みにくいですよね。
static string ToUpper1st2(string value) {
string r = "";
for(int i = 0; i < value.Length; i++) {
r += (i != 0) && (value[i - 1] != ' ') ? value.Substring(i, 1) : value.Substring(i, 1).ToUpper();
}
return r;
}
}
|
がんばってみました。
1 | using System;using System.Linq;class d{static void Main(string[]a){int c=0;a[0].ToCharArray().ToList<char>().ForEach(b=>{if(c!=0&&a[0][c-1]!=' ')Console.Write(b);else Console.Write(b.ToString().ToUpper());c++;});}}
|
でも、こっちの方が短かった。
1 | using System;class P{static void Main(string[]a){for(int i=0;i<a[0].Length;i++){if(i!=0&&a[0][i-1]!=' ')Console.Write(a[0][i]);else Console.Write(a[0][i].ToString().ToUpper());}}}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | using System;
namespace ConsoleApplication
{
class Class
{
[STAThread]
static void Main(string[] args)
{
string aaa = "LL day and night";
string[] st = aaa.Split(' ');
for(int i = 0; i < st.Length; i++)
{
Console.Write(char.ToUpper(st[i][0]) + st[i].Substring(1));
Console.Write(" ");
}
Console.Write("\n");
}
}
}
|




takano32
#6901()
[
Ruby
]
Rating2/10=0.20
文字列に含まれる単語について、それぞれの単語の最初の文字を大文字にしてください。
たとえば、"LL future" と与えられたときは "LL Future" と出力する。"LL day and night" と与えられたときは "LL Day And Night" と出力する。
与えられる文字列はリテラルで表記する、標準入力で与えられる、引数で与えられるなどは自由とします。
余力のあるものはこのプログラムを短くしてみたり、短くしてみたり、短くしてください。
※LL Future実行委員の高野光弘です。この出題は LL Future公式の出題であり、優れたものについてはLL Golfのセッションでご紹介させていただくかもしれません。ご理解の上、ご投稿ください。また、LL Futureのチケットは現在も発売中です。よろしければ、メインイベントの方にもぜひご参加ください。
see: LL Golf
Rating2/10=0.20-0+
[ reply ]