Robel T. Fesshaye

Infinite Innovation

  • Home

Java Strings Part One- 33 Problems with Solutions

January 17, 2016
Problem-1
Given a string name, e.g. “Bob”, return a greeting of the form “Hello Bob!”. 
Example:
helloName(“Bob”) → “Hello Bob!”
helloName(“Alice”) → “Hello Alice!”
helloName(“X”) → “Hello X!”
Solution
public String helloName(String name) {
 return “Hello”+” “+ name+”!”;
}
Problem-2
Given two strings, a and b, return the result of putting them together in the order abba, e.g. “Hi” and “Bye” returns “HiByeByeHi”. 

Example:
makeAbba(“Hi”, “Bye”) → “HiByeByeHi”
makeAbba(“Yo”, “Alice”) → “YoAliceAliceYo”
makeAbba(“What”, “Up”) → “WhatUpUpWhat”
Solution
public String makeAbba(String a, String b) {
return a+b+b+a;

}
Problem-3
The web is built with HTML strings like “<i>Yay</i>” which draws Yay as italic text. In this example, the “i” tag makes <i> and </i> which surround the word “Yay”. Given tag and word strings, create the HTML string with tags around the word, e.g. “<i>Yay</i>”. 

Example:
makeTags(“i”, “Yay”) → “<i>Yay</i>”
makeTags(“i”, “Hello”) → “<i>Hello</i>”
makeTags(“cite”, “Yay”) → “<cite>Yay</cite>”
Solution
public String makeTags(String tag, String word) {
return “<“+tag+”>”+word+”</”+tag+”>”;
}
Problem-4
Given an “out” string length 4, such as “<<>>”, and a word, return a new string where the word is in the middle of the out string, e.g. “<<word>>”. Note: use str.substring(i, j) to extract the String starting at index i and going up to but not including index j. 

Example
makeOutWord(“<<>>”, “Yay”) → “<<Yay>>”
makeOutWord(“<<>>”, “WooHoo”) → “<<WooHoo>>”
makeOutWord(“[[]]”, “word”) → “[[word]]”
Solution
public String makeOutWord(String out, String word) {
 return out.substring(0,2)+word+out.substring(2);
}
Problem-5
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. 

Example:
extraEnd(“Hello”) → “lololo”
extraEnd(“ab”) → “ababab”
extraEnd(“Hi”) → “HiHiHi”
Solution
public String extraEnd(String str) {
String result=str.substring(str.length()-2);
return result+result+result;
}
Problem-6
Given a string, return the string made of its first two chars, so the String “Hello” yields “He”. If the string is shorter than length 2, return whatever there is, so “X” yields “X”, and the empty string “” yields the empty string “”. Note that str.length() returns the length of a string. 

Example:
firstTwo(“Hello”) → “He”
firstTwo(“abcdefg”) → “ab”
firstTwo(“ab”) → “ab”
Solution
public String firstTwo(String str) {
 if(str.length()<=2) return str;
 return str.substring(0,2);
}
Problem-7
Given a string of even length, return the first half. So the string “WooHoo” yields “Woo”. 

Example:
firstHalf(“WooHoo”) → “Woo”
firstHalf(“HelloThere”) → “Hello”
firstHalf(“abcdef”) → “abc”
Solution
public String firstHalf(String str) {
 int mid=str.length()/2;
 return str.substring(0,mid);
}
Problem-8
Given a string, return a version without the first and last char, so “Hello” yields “ell”. The string length will be at least 2. 

Example:
withoutEnd(“Hello”) → “ell”
withoutEnd(“java”) → “av”
withoutEnd(“coding”) → “odin”
Solution
public String withoutEnd(String str) {
return str.substring(1,str.length()-1);
}
Problem-9
Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on the outside and the longer string on the inside. The strings will not be the same length, but they may be empty (length 0). 

Example
comboString(“Hello”, “hi”) → “hiHellohi”
comboString(“hi”, “Hello”) → “hiHellohi”
comboString(“aaa”, “b”) → “baaab”
Solution
public String comboString(String a, String b) {
 String lon=””; // don’t use long and short as variables (reserved by java)
 String shor=””;
  if(a.length()>b.length()){
   lon=a; shor=b;
  }
  else {
  lon=b;shor=a;
  }
  return shor+lon+shor;
}
Problem-10
Given 2 strings, return their concatenation, except omit the first char of each. The strings will be at least length 1. 
Example:
nonStart(“Hello”, “There”) → “ellohere”
nonStart(“java”, “code”) → “avaode”
nonStart(“shotl”, “java”) → “hotlava”
Solution
public String nonStart(String a, String b) {
 String aNew=a.substring(1);
 String bNew=b.substring(1);
 return aNew+bNew;
}
Problem- 11
Given a string, return a “rotated left 2” version where the first 2 chars are moved to the end. The string length will be at least 2. 

Example:
left2(“Hello”) → “lloHe”
left2(“java”) → “vaja”
left2(“Hi”) → “Hi”
Solution
public String left2(String str) {
if(str.length()<=2) return str;
String first=str.substring(0,2);
String left=str.substring(2);
return left+first;
}
Problem-12
Given a string, return a “rotated right 2” version where the last 2 chars are moved to the start. The string length will be at least 2. 

Example:
right2(“Hello”) → “loHel”
right2(“java”) → “vaja”
right2(“Hi”) → “Hi”
Solution
public String right2(String str) {
if(str.length()<=2) return str;
String right=str.substring(str.length()-2);
String first=str.substring(0,str.length()-2);
return right+first;
}
Problem-13
Given a string, return a string length 1 from its front, unless front is false, in which case return a string length 1 from its back. The string will be non-empty.
Example:
theEnd(“Hello”, true) → “H”
theEnd(“Hello”, false) → “o”
theEnd(“oh”, true) → “o”
Solution
public String theEnd(String str, boolean front) {
if(front) return str.substring(0,1);
else{
return str.substring(str.length()-1);
}
}
Problem-14
Given a string, return a version without both the first and last char of the string. The string may be any length, including 0.
Example
withouEnd2(“Hello”) → “ell”
withouEnd2(“abc”) → “b”
withouEnd2(“ab”) → “”
Solution
public String withouEnd2(String str) {
if(str.length()<=2) return “”;
else{
return str.substring(1,str.length()-1);
}
}
Problem-15
Given a string of even length, return a string made of the middle two chars, so the string “string” yields “ri”. The string length will be at least 2.
Example:
middleTwo(“string”) → “ri”
middleTwo(“code”) → “od”
middleTwo(“Practice”) → “ct”
Solution
public String middleTwo(String str) {
int k=str.length()/2;
return str.substring(k-1,k+1);
}
Problem-16
Given a string, return true if it ends in “ly”.
Example
endsLy(“oddly”) → true
endsLy(“y”) → false
endsLy(“oddy”) → false
Solution
public boolean endsLy(String str) {
return(str.length()>=2 && str.substring(str.length()-2).equals(“ly”));
}
Problem-17
Given a string and an int n, return a string made of the first and last n chars from the string. The string length will be at least n.
Example:
nTwice(“Hello”, 2) → “Helo”
nTwice(“Chocolate”, 3) → “Choate”
nTwice(“Chocolate”, 1) → “Ce”
Solution
public String nTwice(String str, int n) {
String front=str.substring(0,n);
String last=str.substring(str.length()-(n));
return front+last;
}
Problem-18
Given a string and an index, return a string length 2 starting at the given index. If the index is too big or too small to define a string length 2, use the first 2 chars. The string length will be at least 2.
Example:
twoChar(“java”, 0) → “ja”
twoChar(“java”, 2) → “va”
twoChar(“java”, 3) → “ja”
Solution
public String twoChar(String str, int index) {
String result=””;
int lim=index+2;
if(index+2>str.length()-1){
lim=str.length();
}
if(index<=0 || index>str.length()-2) result=str.substring(0,2);
else if(index<=str.length()-2) result=str.substring(index,lim);
return result;
}
Problem-19
Given a string of odd length, return the string length 3 from its middle, so “Candy” yields “and”. The string length will be at least 3.
Example
middleThree(“Candy”) → “and”
middleThree(“and”) → “and”
middleThree(“solving”) → “lvi”
Solution
public String middleThree(String str) {
int mid=str.length()/2;
int limit=mid+2;
String result=””;
if(mid+2>str.length()-1) limit=str.length();
result=str.substring(mid-1,limit);
return result;
}
Problem-20
Given a string, return true if “bad” appears starting at index 0 or 1 in the string, such as with “badxxx” or “xbadxx” but not “xxbadxx”. The string may be any length, including 0. Note: use .equals() to compare 2 strings.
Example:
hasBad(“badxx”) → true
hasBad(“xbadxx”) → true
hasBad(“xxbadxx”) → false
Solution
public boolean hasBad(String str) {
if(str.length()>=3 && str.substring(0,3).equals(“bad”)) return true;
else if(str.length()>=4 && str.substring(1,4).equals(“bad”)) return true;
return false;
}
Problem-21
Given a string, return a string length 2 made of its first 2 chars. If the string length is less than 2, use ‘@’ for the missing chars.
Example
atFirst(“hello”) → “he”
atFirst(“hi”) → “hi”
atFirst(“h”) → “h@”
Solution
public String atFirst(String str) {
String result=””;
if(str.length()==0) result=”@@”;
else if(str.length()==1) result=str+”@”;
else if(str.length()>=2) result=str.substring(0,2);
return result;
}
Problem-22
Given 2 strings, a and b, return a new string made of the first char of a and the last char of b, so “yo” and “java” yields “ya”. If either string is length 0, use ‘@’ for its missing char.
Example
lastChars(“last”, “chars”) → “ls”
lastChars(“yo”, “java”) → “ya”
lastChars(“hi”, “”) → “h@”
Solution
public String lastChars(String a, String b) {
if(a.length()==0 && b.length()==0) return “@@”;
else if(a.length()==0) return “@”+b.charAt(b.length()-1);
else if(b.length()==0) return a.charAt(0)+”@”;
return a.substring(0,1)+b.substring(b.length()-1);
}
Problem-23
Given two strings, append them together (known as “concatenation”) and return the result. However, if the concatenation creates a double-char, then omit one of the chars, so “abc” and “cat” yields “abcat”.
Example
conCat(“abc”, “cat”) → “abcat”
conCat(“dog”, “cat”) → “dogcat”
conCat(“abc”, “”) → “abc”
Solution
public String conCat(String a, String b) {
String result=””;
if(a.length()!=0 && b.length()!=0 && a.charAt(a.length()-1)==b.charAt(0)) result=a+b.substring(1);
else result=a+b;
return result;
}
Problem-24
Given a string of any length, return a new string where the last 2 chars, if present, are swapped, so “coding” yields “codign”.
Example
lastTwo(“coding”) → “codign”
lastTwo(“cat”) → “cta”
lastTwo(“ab”) → “ba”
Solution
public String lastTwo(String str) {
String result=””;
int k=str.length();
if(k<=1) result=str;
else if(k>=2){
String first=(str.substring(0,k-2));
String blast=str.substring(k-2,k-1);
String last=str.substring(k-1);
result=first+last+blast;
}
return result;
}
Problem-25
Given a string, if the string begins with “red” or “blue” return that color string, otherwise return the empty string.
Example
seeColor(“redxx”) → “red”
seeColor(“xxred”) → “”
seeColor(“blueTimes”) → “blue”
Solution
public String seeColor(String str) {
//String result=””;
if(str.length()>=3 && str.substring(0,3).equals(“red”)) return str.substring(0,3);
if(str.length()>=4 && str.substring(0,4).equals(“blue”)) return str.substring(0,4);
return “”;
}
Problem-26
Given a string, return true if the first 2 chars in the string also appear at the end of the string, such as with “edited”.
Example
frontAgain(“edited”) → true
frontAgain(“edit”) → false
frontAgain(“ed”) → true
Solution
public boolean frontAgain(String str) {
if(str.length()>=2 && str.substring(0,2).equals(str.substring(str.length()-2))) return true;
return false;
}
Problem-27
Given two strings, append them together (known as “concatenation”) and return the result. However, if the strings are different lengths, omit chars from the longer string so it is the same length as the shorter string. So “Hello” and “Hi” yield “loHi”. The strings may be any length.
Example:
minCat(“Hello”, “Hi”) → “loHi”
minCat(“Hello”, “java”) → “ellojava”
minCat(“java”, “Hello”) → “javaello”
Solution
public String minCat(String a, String b) {
String result=””;
int a1=a.length();
int b1=b.length();
if (a1==b1) result=a+b;
else if(a1>b1) result=a.substring(a.length()-b1)+b;
else if(b1>a1) result=a+b.substring(b.length()-a1);
return result;
}
Problem-28
Given a string, return a new string made of 3 copies of the first 2 chars of the original string. The string may be any length. If there are fewer than 2 chars, use whatever is there.
Example
extraFront(“Hello”) → “HeHeHe”
extraFront(“ab”) → “ababab”
extraFront(“H”) → “HHH”
Solution
public String extraFront(String str) {
String result=””;
if(str.length()<=2) result=str+str+str;
else{
String k=str.substring(0,2);
result=k+k+k;
}
return result;
}
Problem-29
Given a string, if a length 2 substring appears at both its beginning and end, return a string without the substring at the beginning, so “HelloHe” yields “lloHe”. The substring may overlap with itself, so “Hi” yields “”. Otherwise, return the original string unchanged.
Example
without2(“HelloHe”) → “lloHe”
without2(“HelloHi”) → “HelloHi”
without2(“Hi”) → “”
Solution
public String without2(String str) {
String result=””;
if(str.length()<2) return str;
else if(str.substring(0,2).equals(str.substring(str.length()-2))) result=str.substring(2);
else if(!str.substring(0,2).equals(str.substring(str.length()-2))) result=str;
return result;
}
Problem-30
Given a string, return a version without the first 2 chars. Except keep the first char if it is ‘a’ and keep the second char if it is ‘b’. The string may be any length. Harder than it looks.
Example
deFront(“Hello”) → “llo”
deFront(“java”) → “va”
deFront(“away”) → “aay”
Solution
public String deFront(String str) {
String result=””;
String a1=str.substring(0,1);
String b1=str.substring(1,2);
if(a1.equals(“a”) && b1.equals(“b”)) result=str;
else if(a1.equals(“a”) && !b1.equals(“b”)) result=a1+str.substring(2);
else if(!a1.equals(“a”) && b1.equals(“b”)) result=str.substring(1);
else if(!a1.equals(“a”) && !b1.equals(“b”)) result=str.substring(2);
return result;
}
Problem-31
Given a string and a second “word” string, we’ll say that the word matches the string if it appears at the front of the string, except its first char does not need to match exactly. On a match, return the front of the string, or otherwise return the empty string. So, so with the string “hippo” the word “hi” returns “hi” and “xip” returns “hip”. The word will be at least length 1.
Example
startWord(“hippo”, “hi”) → “hi”
startWord(“hippo”, “xip”) → “hip”
startWord(“hippo”, “i”) → “h
Solution
public String startWord(String str, String word) {
String result=””;
int k=word.length();
int m=str.length();
if(k>m) return result;
if(k>=2){
if(str.substring(1,k).equals(word.substring(1))) result=str.substring(0,k);
}
if(k<2) result=str.substring(0,1);
return result;
}
Problem-32
Given a string, if the first or last chars are ‘x’, return the string without those ‘x’ chars, and otherwise return the string unchanged.
Example:
withoutX(“xHix”) → “Hi”
withoutX(“xHi”) → “Hi”
withoutX(“Hxix”) → “Hxi”

Solution
public String withoutX(String str) {
String result=””;
int k=str.length();
if(k==0) result=””;
for(int i=0;i<k;i++){
if(str.charAt(i)==’x’ && (i==0 || i==k-1)) continue;
result+=str.charAt(i);
}
return result;
}
Problem-33
Given a string, if one or both of the first 2 chars is ‘x’, return the string without those ‘x’ chars, and otherwise return the string unchanged. This is a little harder than it looks.
Example
withoutX2(“xHi”) → “Hi”
withoutX2(“Hxi”) → “Hi”
withoutX2(“Hi”) → “Hi”
Solution
public String withoutX2(String str) {
String result=str;
int k=str.length();
if (k==0 || k==1 && str.substring(0).equals(“x”)) result=””;
else if(k>=2){
if(str.substring(0,2).equals(“xx”)) result=str.substring(2);
else if(str.substring(0,1).equals(“x”)) result=str.substring(1);
else if (str.substring(1,2).equals(“x”)) result=str.substring(0,1)+str.substring(2);
}
return result;
}
Another Solution
public String withoutX2(String str) {
String result=””;
int k=str.length();
if(k>=2){
for(int i=0; i<k;i++){
if(str.charAt(i)==’x’ && (i==0 || i==1)) continue;
result+=str.charAt(i);
}
}
return result;
}
** All Problems were taken from the website CodingBat
Posted in: Java Tagged: Strings, Substrings
← Java Basics Part Two -16 Problems with Solutions
Java Arrays Part One-26 Problems with Solutions →

Recent Posts

  • How I Passed the AWS SysOps – Associate (SOA-C02) Exam
  • How I Passed the Certified Kubernetes Application Developer (CKAD) Exam
  • How I Passed the Certified Kubernetes Administrator (CKA) Exam
  • How I passed the AWS Solutions Architect (Associate) Exam

Categories

  • App Dev (4)
  • AWS Solutions Architect – Associate (6)
  • Certifications (5)
  • Electrical Engineering (1)
  • Java (7)
  • PHP & SQL (2)
  • Windows OS (4)

Archives

Tags

#Apache #BlockPorn #DualScreen #Fonts #OpenDNS #Port80 Arrays autosave Electron SQLServer Strings Substrings

Copyright © 2025 Robel T. Fesshaye.

Me WordPress Theme by themehall.com