Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class WordCountInText
- {
- //Write a program that reads a string from the console and lists all different words in the string along with information how many times each word is found.
- static void Main()
- {
- string text = "Write a program that reads a string from the console! It should list all different words in the string, along with information how many times each word is found.";
- text = text.ToLower();
- char[] separators = { ' ', ',', '.', '!', '?', ':', ';', '/', '\\', };
- string[] wordsArray = text.Split(separators,StringSplitOptions.RemoveEmptyEntries);
- Array.Sort(wordsArray);
- int counter = 1;
- string currentWordToCount = wordsArray[0];
- for (int i = 1; i < wordsArray.Length; i++)
- {
- if (wordsArray[i] == currentWordToCount)
- {
- counter++;
- }
- else
- {
- Console.WriteLine("{0,-15} : {1,3}", currentWordToCount, counter);
- currentWordToCount = wordsArray[i];
- counter = 1;
- }
- }
- Console.WriteLine("{0,-15} : {1,3}", currentWordToCount, counter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement