I continued my work on Strings and Arrays, and today I did mostly strings. I did problems from String 1, and one problem from Array 1. The problems are rather simple, but since I'm not used to strings and arrays, they take me a while to complete.
Since the problems are short, I'll do two problems, one string and the other will be the array that I did.
Here is the problem for today:
Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2.
extraEnd("Hello") → "lololo"
extraEnd("ab") → "ababab"
extraEnd("Hi") → "HiHiHi"
public String extraEnd(String str) {
return ((str.substring(str.length() -2, str.length())) + (str.substring(str.length()-2, str.length())) + (str.substring(str.length()-2, str.length())));
//((str.substring(str.length() -2, str.length()))
//here is what we are saying: return only the last two letter of the string - string length -2 up until the full //string- and then times 3, because we want the last part three times.
}
------------------------------------------------------------------------------------------------------------------------------
Given an array of ints, return true if the array is length 1 or more, and the first element and the last element are equal. sameFirstLast({1, 2, 3}) → false sameFirstLast({1, 2, 3, 1}) → true sameFirstLast({1, 2, 1}) → true |
Next class I hope to continue working on strings and arrays. |
No comments:
Post a Comment