/src/string1/String1.java

https://github.com/SergeyKandalintsev/CodingBat · Java · 500 lines · 114 code · 77 blank · 309 comment · 22 complexity · fd8efc784349bc963a712353eabaa6ba MD5 · raw file

  1. package string1;
  2. /**
  3. * Basic string problems -- no loops. Use + to combine Strings, str.length() is the number of chars in a String,
  4. * str.substring(i, j) extracts the substring starting at index i and running up to but not including index j.
  5. * <p/>
  6. * Date: 29.06.13
  7. *
  8. * @author Sergey Kandalintsev
  9. */
  10. public class String1 {
  11. /**
  12. * Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!".
  13. * <ul>
  14. * <li>helloName("Bob") --> "Hello Bob!"</li>
  15. * <li>helloName("Alice") --> "Hello Alice!"</li>
  16. * <li>helloName("X") --> "Hello X!"</li>
  17. * </ul>
  18. *
  19. * @param name
  20. * given string
  21. *
  22. * @return a greeting of the form "name Bob!"
  23. */
  24. public static String helloName(String name) {
  25. return "Hello " + name + "!";
  26. }
  27. /**
  28. * Given two strings, a and b, return the result of putting them together in the order abba, e.g. "Hi" and "Bye"
  29. * returns "HiByeByeHi".
  30. * <ul>
  31. * <li>makeAbba("Hi", "Bye") --> "HiByeByeHi"</li>
  32. * <li>makeAbba("Yo", "Alice") --> "YoAliceAliceYo"</li>
  33. * <li>makeAbba("What", "Up") --> "WhatUpUpWhat"</li>
  34. * </ul>
  35. *
  36. * @param a
  37. * given string
  38. * @param b
  39. * given string
  40. *
  41. * @return the result of putting strings together in the order abba
  42. */
  43. public static String makeAbba(String a, String b) {
  44. return a + b + b + a;
  45. }
  46. /**
  47. * The web is built with HTML strings like "<i>Yay</i>" which draws Yay as italic text. In this example,
  48. * the "i" tag makes <i> and </i> which surround the word "Yay". Given tag and word strings, create the HTML
  49. * string with tags around the word, e.g. "<i>Yay</i>".
  50. * <ul>
  51. * <li>makeTags("i", "Yay") --> "<i>Yay</i>"</li>
  52. * <li>makeTags("i", "Hello") --> "<i>Hello</i>"</li>
  53. * <li>makeTags("cite", "Yay") --> "<cite>Yay</cite>"</li>
  54. * </ul>
  55. *
  56. * @param tag
  57. * given tag string
  58. * @param word
  59. * given word string
  60. *
  61. * @return the HTML string with tags around the word
  62. */
  63. public static String makeTags(String tag, String word) {
  64. return "<" + tag + ">" + word + "</" + tag + ">";
  65. }
  66. /**
  67. * Given an "out" string length 4, such as "<<>>", and a word, return a new string where the word is in the middle
  68. * of the out string, e.g. "<<word>>". Note: use str.substring(i, j) to extract the String starting at index i
  69. * and going up to but not including index j.
  70. * <ul>
  71. * <li>makeOutWord("<<>>", "Yay") --> "<<Yay>>"</li>
  72. * <li>makeOutWord("<<>>", "WooHoo") --> "<<WooHoo>>"</li>
  73. * <li>makeOutWord("[[]]", "word") --> "[[word]]"</li>
  74. * </ul>
  75. *
  76. * @param out
  77. * "out" string length 4
  78. * @param word
  79. * a word
  80. *
  81. * @return string where the word is in the middle of the out string
  82. */
  83. public static String makeOutWord(String out, String word) {
  84. return out.substring(0, 2) + word + out.substring(2, 4);
  85. }
  86. /**
  87. * Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string
  88. * length will be at least 2.
  89. * <ul>
  90. * <li>extraEnd("Hello") --> "lololo"</li>
  91. * <li>extraEnd("ab") --> "ababab"</li>
  92. * <li>extraEnd("Hi") --> "HiHiHi"</li>
  93. * </ul>
  94. *
  95. * @param str
  96. * given string, length at least 2.
  97. *
  98. * @return a new string made of 3 copies of the last 2 chars of the original string
  99. */
  100. public static String extraEnd(String str) {
  101. String result = str.substring(str.length() - 2);
  102. return result + result + result;
  103. }
  104. /**
  105. * Given a string, return the string made of its first two chars, so the String "Hello" yields "He". If the string
  106. * is shorter than length 2, return whatever there is, so "X" yields "X", and the empty string "" yields the
  107. * empty string "". Note that str.length() returns the length of a string.
  108. * <ul>
  109. * <li>firstTwo("Hello") --> "He"</li>
  110. * <li>firstTwo("abcdefg") --> "ab"</li>
  111. * <li>firstTwo("ab") --> "ab"</li>
  112. * </ul>
  113. *
  114. * @param str
  115. * given string
  116. *
  117. * @return String value according to exercise conditions
  118. */
  119. public static String firstTwo(String str) {
  120. return str.length() > 2 ? str.substring(0, 2) : str;
  121. }
  122. /**
  123. * Given a string of even length, return the first half. So the string "WooHoo" yields "Woo".
  124. * <ul>
  125. * <li>firstTwo("Hello") --> "He"</li>
  126. * <li>firstTwo("abcdefg") --> "ab"</li>
  127. * <li>firstTwo("ab") --> "ab"</li>
  128. * </ul>
  129. *
  130. * @param str
  131. * given string of even length
  132. *
  133. * @return first half of the string
  134. */
  135. public static String firstHalf(String str) {
  136. return str.substring(0, str.length() / 2);
  137. }
  138. /**
  139. * Given a string, return a version without the first and last char, so "Hello" yields "ell". The string length
  140. * will be at least 2.
  141. * <ul>
  142. * <li>withoutEnd("Hello") --> "ell"</li>
  143. * <li>withoutEnd("java") --> "av"</li>
  144. * <li>withoutEnd("coding") --> "odin"</li>
  145. * </ul>
  146. *
  147. * @param str
  148. * given string (at least 2 char length)
  149. *
  150. * @return string without the first and last char
  151. */
  152. public static String withoutEnd(String str) {
  153. return str.substring(1, str.length() - 1);
  154. }
  155. /**
  156. * Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on the outside
  157. * and the longer string on the inside. The strings will not be the same length, but they may be empty (length 0).
  158. * <ul>
  159. * <li>comboString("Hello", "hi") → "hiHellohi"</li>
  160. * <li>comboString("hi", "Hello") → "hiHellohi"</li>
  161. * <li>comboString("aaa", "b") → "baaab"</li>
  162. * </ul>
  163. *
  164. * @param a
  165. * string
  166. * @param b
  167. * string
  168. *
  169. * @return string of the form short+long+short
  170. */
  171. public static String comboString(String a, String b) {
  172. return (a.length() < b.length()) ? (a + b + a) : (b + a + b);
  173. }
  174. /**
  175. * Given 2 strings, return their concatenation, except omit the first char of each. The strings will be at least
  176. * length 1.
  177. * * <ul>
  178. * <li>nonStart("Hello", "There") → "ellohere"</li>
  179. * <li>nonStart("java", "code") → "avaode"</li>
  180. * <li>nonStart("shotl", "java") → "hotlava"</li>
  181. * </ul>
  182. *
  183. * @param a
  184. * string (at least 1 char length)
  185. * @param b
  186. * string (at least 1 char length)
  187. *
  188. * @return concatenation of strings <code>a</code> and <code>b</code>, except omit the first char of each
  189. */
  190. public static String nonStart(String a, String b) {
  191. return a.substring(1) + b.substring(1);
  192. }
  193. /**
  194. * Given a string, return a "rotated left 2" version where the first 2 chars are moved to the end. The string
  195. * length will be at least 2.
  196. * <ul>
  197. * <li>left2("Hello") → "lloHe"</li>
  198. * <li>left2("java") → "vaja"</li>
  199. * <li>left2("Hi") → "Hi"</li>
  200. * </ul>
  201. *
  202. * @param str
  203. * given string (at least 2 char length)
  204. *
  205. * @return "rotated left 2" version of given string
  206. */
  207. public static String left2(String str) {
  208. return str.substring(2) + str.substring(0, 2);
  209. }
  210. /**
  211. * Given a string, return a "rotated right 2" version where the last 2 chars are moved to the start. The string
  212. * length will be at least 2.
  213. * <ul>
  214. * <li> right2("Hello") → "loHel"</li>
  215. * <li>right2("java") → "vaja"</li>
  216. * <li>right2("Hi") → "Hi"</li>
  217. * </ul>
  218. *
  219. * @param str
  220. * given string (at least 2 char length)
  221. *
  222. * @return "rotated right 2" version of given string
  223. */
  224. public static String right2(String str) {
  225. return str.substring(str.length() - 2) + str.substring(0, str.length() - 2);
  226. }
  227. /**
  228. * Given a string, return a string length 1 from its front, unless front is false, in which case return a string
  229. * length 1 from its back. The string will be non-empty.
  230. * <ul>
  231. * <li>theEnd("Hello", true) → "H"</li>
  232. * <li>theEnd("Hello", false) → "o"</li>
  233. * <li>theEnd("oh", true) → "o"</li>
  234. * </ul>
  235. *
  236. * @param str
  237. * given string
  238. * @param front
  239. * boolean
  240. *
  241. * @return a string length 1 from the front of given string if front is <code>true</code>. Otherwise returns
  242. * a string length 1 from the back of given string.
  243. */
  244. public static String theEnd(String str, boolean front) {
  245. return front ? str.substring(0, 1) : str.substring(str.length() - 1, str.length());
  246. }
  247. /**
  248. * Given a string, return a version without both the first and last char of the string. The string may be any
  249. * length, including 0.
  250. * <ul>
  251. * <li>withouEnd2("Hello") → "ell"</li>
  252. * <li>withouEnd2("abc") → "b"</li>
  253. * <li>withouEnd2("ab") → ""</li>
  254. * </ul>
  255. *
  256. * @param str
  257. * given string
  258. *
  259. * @return a string without both the first and last char of the string
  260. */
  261. public static String withouEnd2(String str) {
  262. return (str.length() <= 2) ? "" : str.substring(1, str.length() - 1);
  263. }
  264. /**
  265. * Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri".
  266. * The string length will be at least 2.
  267. * <ul>
  268. * <li>middleTwo("string") → "ri"</li>
  269. * <li>middleTwo("code") → "od"</li>
  270. * <li>middleTwo("Practice") → "ct"</li>
  271. * </ul>
  272. *
  273. * @param str
  274. * string of even length
  275. *
  276. * @return a string made of the middle two chars
  277. */
  278. public static String middleTwo(String str) {
  279. int halfLen = str.length() >> 1;
  280. return str.substring(halfLen - 1, halfLen + 1);
  281. }
  282. // -----------------------------------------------------------------------------------------
  283. /**
  284. * Given a string, if the string begins with "red" or "blue" return that color string, otherwise return the empty
  285. * string.
  286. * <ul>
  287. * <li>seeColor("redxx") --> "red"</li>
  288. * <li>seeColor("xxred") --> ""</li>
  289. * <li>seeColor("blueTimes") --> "blue"</li>
  290. * </ul>
  291. *
  292. * @param str
  293. * given string
  294. *
  295. * @return color ("red" or "blue") or empty string
  296. *
  297. * @throws NullPointerException
  298. * If str is null
  299. */
  300. public static String seeColor(String str) {
  301. if (str.startsWith("red")) {
  302. return "red";
  303. } else if (str.startsWith("blue")) {
  304. return "blue";
  305. } else {
  306. return "";
  307. }
  308. }
  309. /**
  310. * Given a string, return true if the first 2 chars in the string also appear at the end of the string, such as
  311. * with "edited".
  312. * <ul>
  313. * <li>frontAgain("edited") --> true</li>
  314. * <li>frontAgain("edit") --> false</li>
  315. * <li>frontAgain("ed") --> true</li>
  316. * </ul>
  317. *
  318. * @param str
  319. * given string
  320. *
  321. * @return <code>true</code> if the first 2 chars in the string also appear at the end of the string. Otherwise
  322. * returns <code>false</code>
  323. */
  324. public static boolean frontAgain(String str) {
  325. if (str == null || str.length() < 2) {
  326. return false;
  327. }
  328. String front = str.substring(0, 2);
  329. String end = str.substring(str.length() - 2);
  330. return front.equals(end);
  331. }
  332. public static String minCat(String a, String b) {
  333. int minLength = Math.min(a.length(), b.length());
  334. return a.substring(a.length() - minLength) + b.substring(b.length() - minLength);
  335. }
  336. public String extraFront(String str) {
  337. String front = str.length() >= 2 ? str.substring(0, 2) : str;
  338. return front + front + front;
  339. }
  340. /**
  341. * Given a string, if a length 2 substring appears at both its beginning and end, return a string without the
  342. * substring at the beginning, so "HelloHe" yields "lloHe". The substring may overlap with itself, so "Hi"
  343. * yields "". Otherwise, return the original string unchanged.
  344. * <ul>
  345. * <li>without2("HelloHe") → "lloHe"</li>
  346. * <li>without2("HelloHi") → "HelloHi"</li>
  347. * <li>without2("Hi") → ""</li>
  348. * </ul>
  349. *
  350. * @param str
  351. * given string
  352. *
  353. * @return
  354. */
  355. public static String without2(String str) {
  356. if (str.length() < 2) {
  357. return str;
  358. }
  359. String frontSubstring = str.substring(0, 2);
  360. String backSubstring = str.substring(str.length() - 2);
  361. if (frontSubstring.equals(backSubstring)) {
  362. return str.substring(2);
  363. }
  364. return str;
  365. }
  366. /**
  367. * Given a string, return a version without the first 2 chars. Except keep the first char if it is 'a' and keep
  368. * the second char if it is 'b'. The string may be any length. Harder than it looks.
  369. * <ul>
  370. * <li>deFront("Hello") → "llo"</li>
  371. * <li>deFront("java") → "va"</li>
  372. * <li>deFront("away") → "aay"</li>
  373. * </ul>
  374. *
  375. * @param str
  376. *
  377. * @return
  378. */
  379. public static String deFront(String str) {
  380. int len = str.length();
  381. if (len == 0) {
  382. return str;
  383. }
  384. if (len == 1) {
  385. return str.charAt(0) == 'a' ? str : "";
  386. }
  387. if (str.charAt(0) == 'a') {
  388. if (str.charAt(1) == 'b') {
  389. return str;
  390. } else {
  391. return "a" + str.substring(2);
  392. }
  393. }
  394. if (str.charAt(1) == 'b') {
  395. return str.substring(1);
  396. }
  397. return str.substring(2);
  398. }
  399. /**
  400. * Given a string and a second "word" string, we'll say that the word matches the string if it appears at the front
  401. * of the string, except its first char does not need to match exactly. On a match, return the front of the string,
  402. * or otherwise return the empty string. So, so with the string "hippo" the word "hi" returns "hi" and "xip"
  403. * returns "hip". The word will be at least length 1.
  404. * <ul>
  405. * <li>startWord("hippo", "hi") → "hi"</li>
  406. * <li>startWord("hippo", "xip") → "hip"</li>
  407. * <li>startWord("hippo", "i") → "h"</li>
  408. * </ul>
  409. *
  410. * @param str
  411. * @param word
  412. *
  413. * @return
  414. */
  415. public static String startWord(String str, String word) {
  416. String match = word.substring(1);
  417. String front = str.substring(1, match.length());
  418. if (match.equals(front)) {
  419. return str.substring(0, word.length());
  420. }
  421. return "";
  422. }
  423. }