2017-11-16 22 views
2

文字列を引数として受け取り、各文の最初の文字を大文字にした文字列のコピーを返すメソッドでアプリケーションを作成します。各文の最初の文字を大文字にする方法

これは私がこれまでに持っているものであり、私はそれが正しいように見えることはできません。

//Create method to process string. 
    private string Sentences(string input) 
    { 
     //Capitalize first letter of input. 
     char firstLetter = char.ToUpper(input[0]); 

     //Combine the capitalize letter with the rest of the input. 
     input = firstLetter.ToString() + input.Substring(1); 

     //Create a char array to hold all characters in input. 
     char[] letters = new char[input.Length]; 

     //Read the characters from input into the array. 
     for (int i = 0; i < input.Length; i++) 
     { 
      letters[i] = input[i]; 
     } 

     //Loop through array to test for punctuation and capitalize a character 2 index away. 
     for (int index = 0; index < letters.Length; index++) 
     { 
      if(char.IsPunctuation(letters[index])) 
      { 
       if (!((index + 2) >= letters.Length)) 
       { 
        char.ToUpper(letters[index+ 2]); 
       } 
      } 
     } 

     for(int ind = 0; ind < letters.Length; ind++) 
     { 
      input += letters[ind].ToString(); 
     } 

     return input; 
    } 
+0

「私はそれが正しいように見えることはできません。」 - いくつかの詳細が便利です。あなたのコードの問題は何ですか? – CodeFuller

+0

アルゴリズムには少なくとも1つの概念上の問題があります。句読記号の次の文字が2文字離れていない場合はどうなりますか?句読点の後に2つのスペースがあっても、改行文字であってもかまいません。 –

+0

これは宿題のように聞こえますが、文識別のパラメータがわかりますか?それは難しいからです。私がそれをしなければならなかったとき、私はMicrosoft Cognitive Servicesにその文章を発送し、それを私のために分割させました。 – Crowcoder

答えて

0

私はあなたの文章中のすべてのドットを識別するために正規表現を使用することをお勧めします。一致を取得し、大文字にして元の文の中の一致インデックスに置き換えます。実際に.NET上のコードを試してみるIDEはありませんが、理解を深めるためには疑似コードで記述することができます。

String setence = "your.setence.goes.here"; 
    Regex rx = new Regex("/\..*?[A-Z]/"); 
    foreach (Match match in rx.Matches(sentence)) 
    { 
     setence.remove(match.Index, 2).insert(match.Index, String.ToUpper(match.Value)); 
    } 
+0

!それらの後の空白はrxにある必要があります:) –

0

あなたはLinq.Aggregate n個を使用することができます - それはのをどのように動作するかの説明のためのコードとコード出力のコメントを参照してください。

"Bla。blubb"も尊重します。 "。?!"の後の空白をチェックする必要があります。

using System; 
using System.Linq; 

internal class Program 
{ 
    static string Capitalize(string oldSentence) 
    { 
     return 

      // this will look at oldSentence char for char, we start with a 
      // new string "" (the accumulator, short acc) 
      // and inspect each char c of oldSentence 

      // comment all the Console.Writelines in this function, thats 
      // just so you see whats done by Aggregate, not needed for it to 
      // work 

      oldSentence 
      .Aggregate("", (acc, c) => 
      { 
       System.Console.WriteLine("Accumulated: " + acc); 
       System.Console.WriteLine("Cecking:  " + c); 

       // if the accumulator is empty or the last character of 
       // trimmed acc is a ".?!" we append the 
       // upper case of c to it 
       if (acc.Length == 0 || ".?!".Any(p => p == acc.Trim().Last())) // (*) 
        acc += char.ToUpper(c); 
       else 
        acc += c; // else we add it unmodified 

       System.Console.WriteLine($"After:  {acc}\n"); 

       return acc; // this returns the acc for the next iteration/next c     
      }); 
    } 

    static void Main(string[] args) 
    { 
     Console.SetBufferSize(120, 1000); 
     var oldSentence = "This is a testSentence. some occurences " 
      + "need capitalization! for examlpe here. or here? maybe " 
      + "yes, maybe not."; 

     var newSentence = Capitalize(oldSentence); 

     Console.WriteLine(new string('*', 80)); 
     Console.WriteLine(newSentence); 
     Console.ReadLine(); 
    } 
} 

(*)

  • ".?!".Any(p => p == ...))手段は".?!"
  • acc.Trim().Last()...は意味等しい任意の文字を含む文字列を行いますaccの終わりに/前の空白を削除し、私の最後を与えます文字

.Last()および.Any()もLinqです。 LINQの-ESC延長のほとんどは、ここで見つけることができます:https://msdn.microsoft.com/en-us/library/9eekhta0(v=vs.110).aspx

出力(切り取ら - そのかなり長め; O)

Accumulated: 
Cecking:  T 
After:  T 

Accumulated: T 
Cecking:  h 
After:  Th 

Accumulated: Th 
Cecking:  i 
After:  Thi 

Accumulated: Thi 
Cecking:  s 
After:  This 

Accumulated: This 
Cecking: 
After:  This 

Accumulated: This 
Cecking:  i 
After:  This i 

Accumulated: This i 
Cecking:  s 
After:  This is 

<snipp - .. you get the idea how Aggregate works ...> 

Accumulated: This is a testSentence. 
Cecking:  s 
After:  This is a testSentence. S 

<snipp> 

Accumulated: This is a testSentence. Some occurences need capitalization! 
Cecking:  f 
After:  This is a testSentence. Some occurences need capitalization! F 

<snipp> 
******************************************************************************** 
This is a testSentence. Some occurences need capitalization! For examlpe here. Or here? Maybe yes, maybe not. 
0

次の2つのタスクがあります。

1)分割テキストに文 2)文章の最初の文字を大文字にする

タスク1は非常に複雑である可能性があります。そこにたくさんの狂った言語があるからです。これは宿題なので、私はあなたが先に進むことができ、よく分かりやすいセパレータで分割できると仮定します。

タスク2は基本的な文字列操作です。最初の文字を選択し、大文字にして、部分文字列操作で文の欠落部分を追加します。ここで

は、コード例です:

char[] separators = new char[] { '!', '.', '?' }; 
string[] sentencesArray = "First sentence. second sentence!lastone.".Split(separators, StringSplitOptions.RemoveEmptyEntries); 
var i = 0; 
Array.ForEach(sentencesArray, e => 
{ 
sentencesArray[i] = e.Trim().First().ToString().ToUpper() + 
e.Trim().Substring(1); 
i++; 
}); 
関連する問題