Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write a program that counts how many times a given word occurs in given text. The first line in the input holds the word. The second line of the input holds the text. The output should be a single integer number – the number of word occurrences. Matching should be case-insensitive. Note that not all matching substrings are words and should be counted. A word is a sequence of letters separated by punctuation or start / end of text.
- namespace _16.CountingWordInText
- {
- using System;
- public class CountingWordInText
- {
- public static void Main(string[] args)
- {
- checked
- {
- Console.Write("Enter your counting Word: ");
- string countingWord = Console.ReadLine();
- Console.WriteLine("Enter your holds Text on single line!");
- string inputText = Console.ReadLine();
- if (countingWord != string.Empty && inputText != string.Empty)
- {
- char[] separators = new char[] { ' ', ',', ';', '-', ':', '"', '\'', '(', ')', '/', '!', '?', '.', '$', '@', '#', '%', '&', '\\', '[', ']', '{', '}', '|', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '*', '=' };
- string[] text = inputText.Split(separators, StringSplitOptions.RemoveEmptyEntries);
- int countWord = 0;
- for (int i = 0; i < text.Length; i++)
- {
- string currentWord = text[i];
- bool isEquals = countingWord.Equals(currentWord, StringComparison.OrdinalIgnoreCase);
- if (isEquals)
- {
- countWord++;
- }
- }
- Console.WriteLine("The Given word occurs in given text {0} times !", countWord);
- }
- else
- {
- Console.WriteLine("Error! - Invalid Input!!!");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement