Advertisement
AvengersAssemble

SomeSt

Jan 15th, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication4
  7. {
  8.     class Program
  9.     {
  10.         public static bool Found(char[] sentence, char[] word)
  11.         {
  12.             int k = 0;
  13.             for (int i = 0; i < sentence.Length; i++)
  14.             {
  15.                 if (sentence[i] == word[k])
  16.                     k++;
  17.                 else
  18.                     k = 0;
  19.                 if (k == word.Length)
  20.                 {
  21.                     if (i == sentence.Length - 1 || i - word.Length + 1 == 0 || sentence[i + 1] == ' ' && sentence[i - word.Length] == ' ')
  22.                         return true;
  23.                     k = 0;
  24.                     Console.WriteLine(sentence[i]);
  25.                 }
  26.             }
  27.             return false;
  28.         }
  29.         public static int Count(char[] sentence, char[] word)
  30.         {
  31.             int count = 0, k = 0;
  32.             for (int i = 0; i < sentence.Length; i++)
  33.             {
  34.                 if (sentence[i] == word[k])
  35.                     k++;
  36.                 else
  37.                     k = 0;
  38.                 if (k == word.Length)
  39.                 {
  40.                     if (i == sentence.Length - 1 || i - word.Length + 1 == 0 || sentence[i + 1] == ' ' && sentence[i - word.Length] == ' ')
  41.                         count++;
  42.                     k = 0;
  43.                 }
  44.             }
  45.             return count;
  46.         }
  47.         public static void Replace(char[] sentence, char[] word, char[] newWord)
  48.         {
  49.             int k = 0;
  50.             for (int i = 0; i < sentence.Length; i++)
  51.             {
  52.                 if (sentence[i] == word[k])
  53.                     k++;
  54.                 else
  55.                     k = 0;
  56.                 if (k == word.Length)
  57.                 {
  58.                     for (int j = i - k + 1; j < i + 1; j++)
  59.                         sentence[j] = newWord[j + k - i - 1];
  60.                     k = 0;
  61.                 }
  62.             }
  63.         }
  64.         public static void RealReplace(char[] sentence, char[] word, char[] newWord)
  65.         {
  66.             int k = 0;
  67.             int lastChar = 0;
  68.             while (sentence[lastChar] != ' ')
  69.                 lastChar++;
  70.             for (int i = 0; i < lastChar; i++)
  71.             {
  72.                 if (sentence[i] == word[k])
  73.                     k++;
  74.                 else
  75.                     k = 0;
  76.                 if (k == word.Length)
  77.                 {
  78.                     for (int m = lastChar + newWord.Length - word.Length - 1; m > i; m--)
  79.                         sentence[m] = newWord[lastChar + newWord.Length - word.Length - 1 - m];
  80.                     for (int j = i - word.Length + 1; j < i - word.Length + newWord.Length; j++)
  81.                         sentence[j] = newWord[j - i + word.Length - 1];
  82.                     k = 0;
  83.                 }
  84.             }
  85.         }
  86.         static void Main(string[] args)
  87.         {
  88.             char[] arr1 = new char[] { 'a', 'b', 'c', 'x', 'y', 'z', 'f', 'k', 'l', 'm', 'a', 'b', 'c', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
  89.             char[] arr2 = new char[] { 'a', 'b', 'c', 'd' };
  90.             char[] arr3 = new char[] { '1', '2', '3', '4' };
  91.             Console.WriteLine("Is word found? " + Found(arr1, arr2));
  92.             Console.WriteLine("Word appears " + Count(arr1, arr2) + " times");
  93.             Console.WriteLine("Word will now be replaced");
  94.             Replace(arr1, arr2, arr3);
  95.             Console.WriteLine("Updated sentence: ");
  96.             foreach (char c in arr1)
  97.                 Console.Write(c);
  98.             Console.WriteLine();
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement