Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication4
- {
- class Program
- {
- public static bool Found(char[] sentence, char[] word)
- {
- int k = 0;
- for (int i = 0; i < sentence.Length; i++)
- {
- if (sentence[i] == word[k])
- k++;
- else
- k = 0;
- if (k == word.Length)
- {
- if (i == sentence.Length - 1 || i - word.Length + 1 == 0 || sentence[i + 1] == ' ' && sentence[i - word.Length] == ' ')
- return true;
- k = 0;
- Console.WriteLine(sentence[i]);
- }
- }
- return false;
- }
- public static int Count(char[] sentence, char[] word)
- {
- int count = 0, k = 0;
- for (int i = 0; i < sentence.Length; i++)
- {
- if (sentence[i] == word[k])
- k++;
- else
- k = 0;
- if (k == word.Length)
- {
- if (i == sentence.Length - 1 || i - word.Length + 1 == 0 || sentence[i + 1] == ' ' && sentence[i - word.Length] == ' ')
- count++;
- k = 0;
- }
- }
- return count;
- }
- public static void Replace(char[] sentence, char[] word, char[] newWord)
- {
- int k = 0;
- for (int i = 0; i < sentence.Length; i++)
- {
- if (sentence[i] == word[k])
- k++;
- else
- k = 0;
- if (k == word.Length)
- {
- for (int j = i - k + 1; j < i + 1; j++)
- sentence[j] = newWord[j + k - i - 1];
- k = 0;
- }
- }
- }
- public static void RealReplace(char[] sentence, char[] word, char[] newWord)
- {
- int k = 0;
- int lastChar = 0;
- while (sentence[lastChar] != ' ')
- lastChar++;
- for (int i = 0; i < lastChar; i++)
- {
- if (sentence[i] == word[k])
- k++;
- else
- k = 0;
- if (k == word.Length)
- {
- for (int m = lastChar + newWord.Length - word.Length - 1; m > i; m--)
- sentence[m] = newWord[lastChar + newWord.Length - word.Length - 1 - m];
- for (int j = i - word.Length + 1; j < i - word.Length + newWord.Length; j++)
- sentence[j] = newWord[j - i + word.Length - 1];
- k = 0;
- }
- }
- }
- static void Main(string[] args)
- {
- char[] arr1 = new char[] { 'a', 'b', 'c', 'x', 'y', 'z', 'f', 'k', 'l', 'm', 'a', 'b', 'c', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
- char[] arr2 = new char[] { 'a', 'b', 'c', 'd' };
- char[] arr3 = new char[] { '1', '2', '3', '4' };
- Console.WriteLine("Is word found? " + Found(arr1, arr2));
- Console.WriteLine("Word appears " + Count(arr1, arr2) + " times");
- Console.WriteLine("Word will now be replaced");
- Replace(arr1, arr2, arr3);
- Console.WriteLine("Updated sentence: ");
- foreach (char c in arr1)
- Console.Write(c);
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement