2016-03-31 2 views
2

После фрагмента кода после console.clear, где у меня проблемы. Я запускаю свою программу, и появляются слова, заглавные буквы которых выражаются другими битами предложения. Я должен разделить предложение на отдельные слова.Изменение предложения на отдельные слова

Заполните первую букву каждого слова, затем объедините слова обратно в одну переменную. Код выделен полужирным шрифтом

using System; 
using System.Threading.Tasks; 

namespace The_Quick_Brown_Fox 
{ 
    class Program 
    { 
     static void Main() 
     { 

      string copyOne = "the quick brown fox jumps over the lazy dog"; 
      string hairy = "hairy"; 
      string copyTwo; 

      copyTwo = string.Copy(copyOne); 
      copyTwo = copyTwo.Replace("dog", "chicken"); 
      copyTwo = copyTwo.Insert(10, hairy); 
      copyTwo = copyTwo.TrimEnd(); 

      Console.WriteLine(copyOne); 
      Console.WriteLine(); 
      Console.WriteLine("" + copyTwo + ""); 

      Console.ReadLine(); 
      Console.Clear(); 

      string lower = (copyTwo); 
      Console.WriteLine(lower.ToUpper()); 

      Console.ReadLine(); 
      Console.Clear(); 

      string upper = (copyTwo); 
      Console.WriteLine(upper.ToLower()); 

      Console.ReadLine(); 
      Console.Clear(); 

      copyTwo = string.Copy(copyTwo); 
      copyTwo = copyTwo.Replace("e", "y"); 

      Console.WriteLine("" + copyTwo + ""); 

      Console.ReadLine(); 
      Console.Clear(); 

      string[] names = { "Krissi", "Dale", "Bo", "Christopher" }; 
      double[] wealth = { 150000, 1000000, 5.66, 10 }; 

      Console.Write("names".PadRight(15)); 
      Console.WriteLine("wealth".PadLeft(8)); 

      for (int i = 0; i < 4; i++) 
      { 
       Console.Write(names[i].PadRight(15)); 
       Console.WriteLine(wealth[i].ToString().PadLeft(8)); 
      } 

      Console.ReadLine(); 
      Console.Clear(); 

      **string wordThree = "the brown fox jumps over the lazy dog"; 
      string[] split = wordThree.Split(' '); 
      wordThree = wordThree.Replace("dog", "chicken"); 
      wordThree = wordThree.Insert(10, hairy); 
      wordThree = wordThree.TrimEnd(); 

      Console.WriteLine(wordThree); 
      Console.WriteLine(); 
      Console.WriteLine("" + wordThree + ""); 

      foreach (string item in split) 
      { 
      wordThree = wordThree + item[0].ToString().ToUpper() 
       + item.Substring(1) + " "; 
      } 
      wordThree = wordThree.Trim(); 
      Console.WriteLine(wordThree + " "); 
      Console.ReadLine();** 


     } 
    } 
} 
+2

Является ли это домашнее задание? – aguertin

+1

Ну, а что, если? Домашнее задание не вне темы. Осталось только немного информации: текущий выход и желаемый результат – Breeze

+0

Домашняя работа была первой частью кода. Это продвинутый материал для класса –

ответ

0

Я думаю, это решит вашу проблему ?? Я добавил одну новую строковую переменную под названием Finalword, где я добавляю новое предложение, которое вы манипулируете на wordthree. посмотрите на это

static void Main() 
    { 

     string copyOne = "the quick brown fox jumps over the lazy dog"; 
     string hairy = "hairy "; 
     string copyTwo; 

     copyTwo = string.Copy(copyOne); 
     copyTwo = copyTwo.Replace("dog", "chicken"); 
     copyTwo = copyTwo.Insert(10, hairy); 
     copyTwo = copyTwo.TrimEnd(); 

     Console.WriteLine(copyOne); 
     Console.WriteLine(); 
     Console.WriteLine("" + copyTwo + ""); 

     Console.ReadLine(); 
     Console.Clear(); 

     string lower = (copyTwo); 
     Console.WriteLine(lower.ToUpper()); 

     Console.ReadLine(); 
     Console.Clear(); 

     string upper = (copyTwo); 
     Console.WriteLine(upper.ToLower()); 

     Console.ReadLine(); 
     Console.Clear(); 

     copyTwo = string.Copy(copyTwo); 
     copyTwo = copyTwo.Replace("e", "y"); 

     Console.WriteLine("" + copyTwo + ""); 

     Console.ReadLine(); 
     Console.Clear(); 

     string[] names = { "Krissi", "Dale", "Bo", "Christopher" }; 
     double[] wealth = { 150000, 1000000, 5.66, 10 }; 

     Console.Write("names".PadRight(15)); 
     Console.WriteLine("wealth".PadLeft(8)); 

     for (int i = 0; i < 4; i++) 
     { 
      Console.Write(names[i].PadRight(15)); 
      Console.WriteLine(wealth[i].ToString().PadLeft(8)); 
     } 

     Console.ReadLine(); 
     Console.Clear(); 

     string wordThree = "the brown fox jumps over the lazy dog"; 
     wordThree = wordThree.Replace("dog", "chicken"); 
     wordThree = wordThree.Insert(10, hairy); 
     wordThree = wordThree.TrimEnd(); 
     string[] split = wordThree.Split(' '); 
     Console.WriteLine(wordThree); 
     Console.WriteLine(); 
     Console.WriteLine("" + wordThree + ""); 
     string FinalWord = ""; 
     foreach (string item in split) 
     { 
      FinalWord = FinalWord + item[0].ToString().ToUpper() 
      + item.Substring(1) + " "; 
     } 
     FinalWord = FinalWord.Trim(); 
     Console.WriteLine(FinalWord + " "); 
     Console.ReadLine(); 


    } 
0

Следующий код поможет вам преобразовать данные строки в первую букву в верхнем регистре.

string copyThree = string.Join(" ", copyOne.Split(' ').Select(s => s.Length > 0 ? s[0].ToString().ToUpper() + s.Substring(1) : s)); 
0

заглавной первую букву каждого слова, то сцепить слова обратно в одну переменную.

Любой из них должен работать на вас.

var wordThree = string.Join(" ", input.Split(new char[] {' ' }) 
             .Select(s=> s.Substring(0,1).ToUpper() + s.Length > 1? s.Substring(1) : "")) 

Или это, которое хранит другие буквы в lowercase.

var culture = new CultureInfo("en-US"); 

var wordThree = string.Join(" ", wordThree.Split(new char[] {' ' }) 
              .Select(s=> culture.TextInfo.ToTitleCase(s.ToLower()))); 

Работа Example

Смежные вопросы