Advertisement
Guest User

Untitled

a guest
May 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _8_laba
  8. {
  9. class Program
  10. {
  11.  
  12. static void Func1(string s)
  13. {
  14. Console.WriteLine("Введите подстроку: ");
  15. string n = Console.ReadLine();
  16. string[] p = s.Split(' ', ',');
  17.  
  18. for (int i = 0; i < p.Length; i++)
  19. if (p[i].Contains(n)) //проверяет, содержится ли заданная подстрока в строке
  20. Console.WriteLine(p[i]);
  21. }
  22.  
  23. static void Func2(string s)
  24. {
  25. Console.WriteLine("Введите число: ");
  26. int n = int.Parse(Console.ReadLine());
  27. string[] p = s.Split(' ', ',');
  28.  
  29. for (int i = 0; i < p.Length; i++)
  30. if (p[i].Length <= n)
  31. Console.WriteLine(p[i]); //выводит только те слова, которые содержат н букв
  32. }
  33.  
  34. static void Func3(string s)
  35. {
  36. string[] p = s.Split(' ', ',');
  37.  
  38. for (int i = 0; i < p.Length; i++)
  39. if (char.IsUpper(p[i][0])) //выводит слова,начинающиеся с прописной буквы
  40. Console.WriteLine(p[i]);
  41. }
  42.  
  43. static void Func4(string s)
  44. {
  45. string[] p = s.Split(' ', ',');
  46.  
  47. for (int i = 0; i < p.Length; i++)
  48. {
  49. for (int j = 0; j < p[i].Length; j++)
  50. if (char.IsNumber(p[i][j])) //проверка, содержит ли сообщение цифру
  51. {
  52. Console.WriteLine(p[i]);
  53. break;
  54. }
  55. }
  56. }
  57.  
  58. static void Func5(string s)
  59. {
  60. Console.WriteLine("Ввести подстроку: ");
  61. string n = Console.ReadLine();
  62. string[] p = s.Split(' ', ',');
  63.  
  64. for (int i = 0; i < p.Length; i++)
  65. {
  66. if (p[i].EndsWith(n))
  67. p[i] = p[i].Remove(0); //удаляет все слова которые кончаются на заданный символ
  68. Console.Write(p[i] + " ");
  69. }
  70. }
  71.  
  72. static void Func6(string s)
  73. {
  74. Console.Write("Ввести подстроку: ");
  75. char n = char.Parse(Console.ReadLine());
  76. string[] p = s.Split(' ', ',');
  77.  
  78. for (int i = 0; i < p.Length; i++)
  79. {
  80. for (int j = 0; j < p[i].Length; j++)
  81. if (p[i][j] == n) //удаляет все слова, содержащие заданный символ
  82. {
  83. p[i] = p[i].Remove(0);
  84. break;
  85. }
  86. Console.Write(p[i] + " ");
  87. }
  88. }
  89.  
  90. static string Func7(string s)
  91. {
  92. string[] p = s.Split(' ', ',');
  93.  
  94. for (int i = 0; i < p.Length; i++)
  95. {
  96. if (p[i].Length == 1)
  97. {
  98. if (i != p.Length - 1) //удаляет все однобуквенные слова
  99. p[i] = p[i + 1];
  100. else
  101. p[i] = p[i].Remove(0);
  102. }
  103. }
  104.  
  105. return String.Join(" ", p);
  106. }
  107. static string Func8(string s)
  108. {
  109. string[] p = s.Split(' ', ',');
  110. List<string> p1 = new List<string> { };
  111.  
  112. foreach (var i in p)
  113. p1.Add(i);
  114.  
  115. for (int i = 0; i < p1.Count; i++)
  116. {
  117. if (p1[i].Equals(i + 1))
  118. p1.Remove(i + 1);
  119. }
  120.  
  121. foreach (var i in p1)
  122. {
  123. Console.WriteLine(i);
  124. }
  125.  
  126. return String.Join(" ", p1);
  127. }
  128.  
  129. static int Func9(string s)
  130. {
  131. Console.Write("Ввести подстроку: ");
  132. string substr = Console.ReadLine();
  133.  
  134. int count = 0;
  135. string[] words = s.Split(' ', ',');
  136.  
  137. for (int i = 0; i < words.Length; i++)
  138. {
  139. if (words[i].Equals(substr)) //удаляет все повторяющиеся слова в сообщении
  140. count++;
  141. }
  142.  
  143. return count;
  144. }
  145.  
  146. static int Func10(string s)
  147. {
  148. int count = 0;
  149. string[] words = s.Split(' ', ',');
  150.  
  151.  
  152. for (int i = 0; i < words.Length; i++)
  153. {
  154. bool isLower = true;
  155. for (int j = 0; j < words[i].Length; j++)
  156. {
  157. if (!Char.IsLower(words[i][j])) //подсчет слов, состоящих только из прописных букв
  158. {
  159. isLower = false;
  160. break;
  161. }
  162. }
  163. if (isLower)
  164. count++;
  165. }
  166. return count;
  167. }
  168.  
  169. static void Func11(string s)
  170. {
  171. string[] words = s.Split(' ', ',');
  172.  
  173. int max = 0;
  174. for (int i = 0; i < words.Length; i++)
  175. {
  176. if (words[max].Length < words[i].Length) //самое длинное слово сообщения
  177. max = i;
  178. }
  179.  
  180. Console.WriteLine("Index: " + max + ". Word: " + words[max]);
  181. }
  182.  
  183. static void Func12(string s)
  184. {
  185. string[] words = s.Split(' ', ',');
  186.  
  187. int max = 0;
  188. for (int i = 0; i < words.Length; i++) //поиск всеъ самых длинных слов в сообщении
  189. {
  190. if (words[max].Length < words[i].Length)
  191. max = i;
  192. }
  193. for (int i = 0; i < words.Length; i++)
  194. {
  195. if (words[max].Length == words[i].Length)
  196. Console.WriteLine("Index: " + i + ". Word: " + words[i]);
  197. }
  198. }
  199.  
  200. static void Func13(string s)
  201. {
  202. string[] words = s.Split(' ', ',');
  203.  
  204. int max = 0;
  205. for (int i = 0; i < words.Length; i++) //Найти самое короткое слово сообщения
  206. {
  207. if (words[max].Length > words[i].Length)
  208. max = i;
  209. }
  210.  
  211. Console.WriteLine("Index: " + max + ". Word: " + words[max]);
  212. }
  213.  
  214. static void Func14(string s)
  215. {
  216. string[] words = s.Split(' ', ',');
  217.  
  218. int max = 0;
  219. for (int i = 0; i < words.Length; i++) //Найти все самые короткие слова сообщения
  220. {
  221. if (words[max].Length > words[i].Length)
  222. max = i;
  223. }
  224. for (int i = 0; i < words.Length; i++)
  225. {
  226. if (words[max].Length == words[i].Length)
  227. Console.WriteLine("Index: " + i + ". Word: " + words[i]);
  228. }
  229. }
  230.  
  231. static void Func16(string s)
  232. {
  233. string[] words = s.Split(' ', ',');
  234. List<string> wordsout = new List<string> { };
  235. int count = 0;
  236.  
  237. for (int i = 0; i < words.Length; i++) //Вывести только те слова, которые встречаются в тексте ровно один раз
  238. {
  239. for (int j = 0; j < words.Length; j++)
  240. {
  241. if (words[i] == words[j] && i != j)
  242. count++;
  243. }
  244. if (count == 0)
  245. wordsout.Add(words[i]);
  246. count = 0;
  247. }
  248. for (int i = 0; i < wordsout.Count; i++)
  249. Console.WriteLine(wordsout[i]);
  250. }
  251.  
  252. static void Func17(string s)
  253. {
  254. string[] words = s.Split(' ', ',');
  255. List<string> wordsout = new List<string> { };
  256. int count = 1;
  257.  
  258. for (int i = 0; i < words.Length; i++) //Вывести только те слова, которые встречаются более n раз.
  259. {
  260. for (int j = 0; j < words.Length; j++)
  261. {
  262. if (words[i] == words[j] && i != j)
  263. count++;
  264. }
  265. if (count > 1)
  266. wordsout.Add(words[i]);
  267. count = 1;
  268. }
  269. for (int i = 0; i < wordsout.Count; i++)
  270. Console.WriteLine(wordsout[i]);
  271. }
  272.  
  273. static string Func18(string s)
  274. {
  275. string[] words = s.Split(' ', ',');
  276.  
  277. string tmp;
  278. for (int i = 0; i < words.Length; i++) //Вывести слова сообщения в алфавитном порядке.
  279. {
  280. for (int j = i + 1; j < words.Length; j++)
  281. {
  282. if (String.Compare(words[i], words[j]) == 1)
  283. {
  284. tmp = words[i];
  285. words[i] = words[j];
  286. words[j] = tmp;
  287. }
  288. }
  289. }
  290.  
  291. return String.Join(" ", words);
  292. }
  293.  
  294. static string Func19(string s)
  295. {
  296. string[] words = s.Split(' ', ',');
  297.  
  298. string tmp;
  299. for (int i = 0; i < words.Length; i++) //Вывести слова сообщения в порядке возрастания их длин
  300. {
  301. for (int j = i + 1; j < words.Length; j++)
  302. {
  303. if (words[i].Length > words[j].Length)
  304. {
  305. tmp = words[i];
  306. words[i] = words[j];
  307. words[j] = tmp;
  308. }
  309. }
  310. }
  311.  
  312. return String.Join(" ", words);
  313. }
  314.  
  315. static void Main(string[] args)
  316. {
  317. Console.Write("Ввести строку: ");
  318. string str = Console.ReadLine();
  319.  
  320. StringBuilder strB = new StringBuilder(str);
  321.  
  322. //Func1(str);
  323. //Func2(str);
  324. //Func3(str);
  325. //Func4(str);
  326. //Func5(str);
  327. //Func6(str);
  328. //Console.WriteLine(Func7(str));
  329. //Console.WriteLine(Func8(str));
  330. Console.WriteLine(Func9(str));
  331. //Console.WriteLine(Func10(str));
  332. //Func11(str);
  333. //Func12(str);
  334. //Func13(str);
  335. //Func14(str);
  336. //Func16(str);
  337. //Func17(str);
  338. //Console.WriteLine(Func18(str));
  339. //Console.WriteLine(Func19(str));
  340.  
  341. Console.ReadKey();
  342. }
  343. }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement