Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. #include <iostream>
  2. int str_len(char* str);//количество символов в строке
  3. int str_cmp(char* str1, char* str2);//сравнивает 2 строки
  4. char* str_cpy(char* str1, char* str2);//копирует из 1 во 2
  5. char* str_cat(char* str1, char* str2);//добавляет к первой вторую
  6. int chr_str(char* str,char ch);//поиск символа в строке
  7. char* str_str(char* str1, char* str2);//поиск строки в строке ,возвращает адрес начала повторения ???
  8. int sum_of_decimals(char* str);//oo111ooo111->222 ???
  9. int sum_of_digits(char* str);//ooo123oo4->10
  10. int words_count(char* str);// подсчет слов с учетом разделителей ";,.!_- ???
  11. int longest(char* str);//самое длинное слово ???
  12. int shortest(char* str);//само короткое слово
  13. char* remove_dup(char* str);//удаляет повторение
  14. char* remove_char(char* str, char ch);//удаляет выборочный символ
  15. char* reverse(char* str);//переворот строки
  16. char* reverse_sub(char* str, int start, int end);//переворот подстроки
  17. void swap(char* a, char* b);//обычный свап лол
  18. char* reverse_words(char* str);
  19. void print_words(char* str);//каждое слово с новой строки
  20. char* remove_dup(char* str);//abcabc->abc ???
  21.  
  22.  
  23. int main()
  24. {
  25. char ch = 'a';
  26. char str1[100] = "hkav56o44lox";
  27. char str2[100] = "kavo";
  28. char str3[100] = "Hello.My name is Ilya t da";
  29. //printf("symbols in string %d", str_len(str2));
  30. //str_cpy(str1, str2);
  31. //str_cat(str1, str2);
  32. //printf("%d",str_cmp(str1, str2));
  33. //printf("%s", str1);
  34. //printf("%d",chr_str(str1, ch));
  35. //printf("%d", str_str(str1, str2));
  36. //printf("%d", sum_of_digits(str1));
  37. //printf("%d", sum_of_decimals(str1));
  38. //printf("%d", words_count(str3));
  39. //printf("%d", longest(str3));
  40. //printf("%d", shortest(str3));
  41. //printf("%s", remove_char(str3,ch));
  42. //printf("%s", reverse(str1));
  43. print_words(str3);
  44. return 0;
  45. }
  46.  
  47. int str_len(char* str)
  48. {
  49. int i = 0;
  50. while (str[i])
  51. {
  52. i++;
  53. }
  54. return i;
  55. }
  56.  
  57.  
  58.  
  59. char* str_cpy(char* str1, char* str2)
  60. {
  61. int i = 0;
  62. while (str2[i])
  63. {
  64. str1[i] = str2[i++];
  65.  
  66. }
  67. str1[i] = '\0';
  68.  
  69. return str1;
  70. }
  71.  
  72. char* str_cat(char* str1, char* str2)
  73. {
  74. int i = 0,j = 0;
  75. while (str1[i])
  76. {
  77. i++;
  78. }
  79. while (str2[j])
  80. {
  81. str1[i + j] = str2[j];
  82. j++;
  83. }
  84. return str1;
  85. }
  86. int str_cmp(char* str1, char* str2) {
  87. int i = 0;
  88. while (str1[i] && str2[i]) {
  89. i++;
  90. }
  91. if ((str1[i] - str2[i])) return 1; else return 0;
  92. }
  93.  
  94. int chr_str(char* str, char ch)
  95. {
  96. int i = 0;
  97. while (str[i]) {
  98. if (str[i] == ch)
  99. {
  100. return i;
  101. }
  102. else i++;
  103. }
  104. return -1;
  105. }
  106.  
  107. char* str_str(char* str1, char* str2)
  108. {
  109. int i = 0, j = 0, flag = 1;
  110. while (str1[i])
  111. {
  112. flag = 1;
  113. j = 0;
  114. while (str2[j])
  115. {
  116. if (str1[i+j]!=str2[j])
  117. {
  118. flag = 0;
  119. }
  120. j++;
  121. }
  122. if (flag) return (&str1[i]);
  123. i++;
  124. }
  125.  
  126. return NULL;
  127. }
  128.  
  129. int sum_of_digits(char* str)
  130. {
  131. int sum=0, i=0;
  132. while (str[i])
  133. {
  134. if ((str[i]>='0')&&(str[i]<='9'))
  135. {
  136. sum = sum + (str[i] - '0');
  137. }
  138. i++;
  139. }
  140. return sum;
  141. }
  142.  
  143. int sum_of_decimals(char* str)
  144. {
  145. int sum = 0, i = 0, dec =0;
  146. while (str[i])
  147. {
  148. if ((str[i] >= '0') && (str[i] <= '9'))
  149. {
  150. dec *= 10;
  151. dec += (str[i] - '0');
  152. }
  153. else
  154. {
  155. sum += dec;
  156. dec = 0;
  157. }
  158. i++;
  159.  
  160. }
  161. sum += dec;
  162. return sum;
  163. }
  164.  
  165. int words_count(char* str)
  166. {
  167. int i = 0, counter = 0, in_word = 1;
  168. char sep[]= "., \n\t()!?";
  169. while (str[i])
  170. {
  171. if ((chr_str(sep, str[i])) != -1)
  172. {
  173. if (in_word)
  174. {
  175. counter++;
  176. in_word = 0;
  177. }
  178. }
  179. else
  180. {
  181. in_word = 1;
  182. }
  183. i++;
  184.  
  185. }
  186. if (in_word)
  187. {
  188. counter++;
  189. }
  190. return counter;
  191. }
  192.  
  193. int longest(char* str)
  194. {
  195. int i = 0, max_len = 0, current = 0, in_word = 1;
  196. char sep[] = "., \n\t()!?";
  197. while (str[i])
  198. {
  199. if ((chr_str(sep, str[i])) != -1)
  200. {
  201. if (in_word)
  202. {
  203. if (current > max_len)
  204. {
  205. max_len = current;
  206. }
  207. current = 0;
  208. in_word = 0;
  209. }
  210. }
  211. else
  212. {
  213. current++;
  214. in_word = 1;
  215. }
  216. i++;
  217. }
  218. if (in_word)
  219. {
  220. if (current > max_len)
  221. {
  222. current = max_len;
  223. }
  224. }
  225. return max_len;
  226. }
  227.  
  228. int shortest(char* str)
  229. {
  230. int i = 0, min_len =100, in_word = 1, current = 0;
  231. char sep[] = "., \n\t()!?";
  232. while (str[i])
  233. {
  234. if ((chr_str(sep, str[i])) != -1)
  235. {
  236. if (in_word)
  237. {
  238. if (current < min_len)
  239. {
  240. min_len = current;
  241. }
  242. current = 0;
  243. in_word = 0;
  244. }
  245. }
  246. else
  247. {
  248. current++;
  249. in_word = 1;
  250. }
  251. i++;
  252. }
  253. if (in_word)
  254. {
  255. if (current < min_len)
  256. {
  257. min_len = current;
  258. }
  259. }
  260. return min_len;
  261. }
  262.  
  263. char* remove_char(char* str, char ch)
  264. {
  265. int i = 0,j=0;
  266. while (str[i])
  267. {
  268. if (str[i] != ch)
  269. {
  270. str[j] = str[i];
  271. j++;
  272. }
  273. i++;
  274. }
  275. str[j] = '\0';
  276. return str;
  277. }
  278.  
  279. char* reverse_sub(char* str, int start, int end)
  280. {
  281. while (start < end)
  282. {
  283. swap(str + start++, str + end--);
  284. }
  285. return str;
  286. }
  287.  
  288. void swap(char* a, char* b)
  289. {
  290. char temp = *a;
  291. *a = *b;
  292. *b = temp;
  293. return;
  294. }
  295.  
  296. char* reverse(char* str)
  297. {
  298. return reverse_sub(str, 0, str_len(str) - 1);
  299. }
  300.  
  301. char* reverse_words(char* str)
  302. {
  303. int i = 0, in_word = 1;start=0
  304. char sep[] = "., \n\t()!?";
  305. while (str[i])
  306. {
  307. if ((chr_str(sep, str[i])) != -1)
  308. {
  309. if (in_word)
  310. {
  311. reverse_sub(str, start, i - 1);
  312. in_word = 0;
  313. }
  314. }
  315. else
  316. {
  317. if (!in_word)
  318. {
  319. start = i
  320. }
  321. in_word = 1;
  322. }
  323. i++;
  324. }
  325. if (in_word)
  326. {
  327. reverse_sub(str, start, i - 1);
  328. }
  329. return str;
  330. }
  331.  
  332. char* remove_dup(char* str)
  333. {
  334. int i = 0, j = 0,flag=1, count = 0;
  335. while (str[i])
  336. {
  337. j = 0;
  338. flag = 1;
  339. while (j < count)
  340. {
  341. if (str[i] == str[j])
  342. {
  343. flag = 0;
  344. break;
  345. }
  346. j++;
  347. }
  348. if (flag)
  349. {
  350. str[count] = str[i];
  351. count++;
  352. }
  353. i++;
  354. }
  355. str[count] = '\0';
  356. return str;
  357. }
  358.  
  359. void print_words(char* str)
  360. {
  361. int i = 0, in_word = 1;
  362. char sep[] = "., \n\t()!?";
  363. while (str[i])
  364. {
  365. if ((chr_str(sep, str[i])) != -1)
  366. {
  367. if (in_word)
  368. {
  369. putchar('\n');
  370. in_word = 0;
  371. }
  372. }
  373. else
  374. {
  375. putchar(str[i])
  376. in_word = 1;
  377. }
  378. i++;
  379. }
  380. if (in_word)
  381. {
  382. putchar('\n');
  383. }
  384. return;
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement