Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Task1
  4. {
  5. public class Sentence
  6. {
  7. public Sentence(string sentence)
  8. {
  9. this.sentence = sentence;
  10. words = sentence.Split(new Char [] {' ', ',', '.', ':', '\t', '!', '?' });
  11. }
  12.  
  13. public int calculateWordsCount()
  14. {
  15. return words.Length;
  16. }
  17. public string findLongestWord()
  18. {
  19. foreach(string word in words)
  20. {
  21. if(word.Length>longestword.Length)
  22. longestword = word;
  23. }
  24. return longestword;
  25. }
  26. public string[] words {get; set;}
  27. public string sentence {get; set;}
  28. public string longestword {get; set;} = "";
  29. public int wordsCount {get; set;}
  30. }
  31. class Program
  32. {
  33. static void Main(string[] args)
  34. {
  35. int n, indexMostWords=0, indexLongestWord=0;
  36. Console.Write("Enter n: ");
  37. n = Int32.Parse(Console.ReadLine());
  38.  
  39. Sentence[] sentences = new Sentence[n];
  40. for(int i=0; i<n;i++)
  41. {
  42. sentences[i] = new Sentence(Console.ReadLine());
  43. }
  44. for(int i=0;i<n;i++)
  45. {
  46. if(sentences[i].findLongestWord().Length>sentences[indexLongestWord].findLongestWord().Length)
  47. {
  48. indexLongestWord = i;
  49. }
  50. if(sentences[i].calculateWordsCount()>sentences[indexMostWords].calculateWordsCount())
  51. {
  52. indexMostWords = i;
  53. }
  54. }
  55. Console.WriteLine("Sentence with most words is: \"{0}\"",sentences[indexMostWords].sentence);
  56. Console.WriteLine("Sentence with longest word is: \"{0}\"",sentences[indexLongestWord].sentence);
  57.  
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement