Problem Statement
SMS messages are short messages sent between mobile phones. The maximum length of a single message is 160 characters, so it is often necessary to abbreviate words.
You are given a String text, and your task is to translate it to SMS language according to the following rules:
Remove all punctuation symbols ('.', ',', '?' and '!').
Replace all uppercase letters with their lowercase equivalents.
Replace all occurrences of "and" with '&'.
Replace all occurrences of "ate" with '8'.
Replace all occurrences of "at" with '@'.
Replace all occurrences of "you" with 'U'.
All quotes are for clarity only. The rules must be applied in the order they appear in the list. For example, "I HATE rats, and you?" will be translated to "i h8 r@s & U".
Return the resulting translation as a String.
Definition
Class: SMSLanguage
Method: translate
Parameters: String
Returns: String
Method signature: String translate(String text)
(be sure your method is public)
Constraints
- text will contain between 1 and 50 characters, inclusive.
- text will contain only letters ('a'-'z', 'A'-'Z'), the characters ',', '.', '!', '?', and spaces (' ').
Examples
0)
"I HATE rats, and you?"
Returns: "i h8 r@s & U"
The example from the problem statement.
1)
"What is the weather like today?"
Returns: "wh@ is the we@her like today"
2)
"It is not too late to.."
Returns: "it is not too l8 to"
3)
"this text is already in sms language"
Returns: "this text is already in sms language"
public class SMSLanguage {
public String translate(String text){
String s1 = text.replaceAll("\\.", "").
replaceAll("\\!", "").
replaceAll("\\?", "").
replaceAll("\\,", "");
String s2 = s1.toLowerCase().
replaceAll("you", "U").
replaceAll("and", "&").
replaceAll("ate", "8")
.replaceAll("at", "@");
return s2;
}
}
*********************************************************************************
Problem Statement
Some texts contain hidden messages. In the context of this problem, the hidden message of a text is composed of the first letter from each word in the text, in the order they appear.
Given a String text, consisting of only lowercase letters and spaces, return the hidden message. A word is a maximal sequence of consecutive letters. There may be multiple spaces between words. Also, text may contain only spaces.
Definition
Class: HiddenMessage
Method: getMessage
Parameters: String
Returns: String
Method signature: String getMessage(String text)
(be sure your method is public)
Constraints
- text will contain between 1 and 50 characters, inclusive.
- Each character of text will be either a lowercase letter ('a'-'z'), or a space (' ').
Examples
0)
"compete online design event rating"
Returns: "coder"
Taking the first letter from each word yields the return "coder".
1)
" c o d e r "
Returns: "coder"
Watch out for the leading spaces.
2)
"round elimination during onsite contest"
Returns: "redoc"
"coder" written backwards.
3)
" "
Returns: ""
Since there are no words here, the empty string must be returned.
import java.util.StringTokenizer;
Problem Statement
Some texts contain hidden messages. In the context of this problem, the hidden message of a text is composed of the first letter from each word in the text, in the order they appear.
Given a String text, consisting of only lowercase letters and spaces, return the hidden message. A word is a maximal sequence of consecutive letters. There may be multiple spaces between words. Also, text may contain only spaces.
Definition
Class: HiddenMessage
Method: getMessage
Parameters: String
Returns: String
Method signature: String getMessage(String text)
(be sure your method is public)
Constraints
- text will contain between 1 and 50 characters, inclusive.
- Each character of text will be either a lowercase letter ('a'-'z'), or a space (' ').
Examples
0)
"compete online design event rating"
Returns: "coder"
Taking the first letter from each word yields the return "coder".
1)
" c o d e r "
Returns: "coder"
Watch out for the leading spaces.
2)
"round elimination during onsite contest"
Returns: "redoc"
"coder" written backwards.
3)
" "
Returns: ""
Since there are no words here, the empty string must be returned.
import java.util.StringTokenizer;
public class HiddenMessage {
public static void main(String[] args) {
System.out.println(getMessage(" c od e r"));
}
public static String getMessageOld(String text) {
String param[] = text.split(" ");
String res = "";
for (String r : param) {
if (r.length() > 0)
r = r.substring(0, 1);
res = res + r;
}
return res;
}
public static String getMessage(String text) {
String res = "";
StringTokenizer st = new StringTokenizer(text);
while(st.hasMoreTokens()){
res = res+ st.nextToken().charAt(0);
}
return res;
}
}
*********************************************************************************
Problem Statement
You are working for the financial institution TopBank, and you have been tasked with writing a module that will take an initial account balance, along with a list of that day's transactions, and return the ending balance for the day.
Each transaction will be either a credit, which adds funds to the account, or a debit, which removes funds from the account. If a debit exceeds the available funds at the time, then the account balance will go negative. You will be given an int startingBalance, the initial account balance. You will also be given a String[] transactions, the list of transactions for the day. Each element of transactions will be in the form "type amount" (quotes added for clarity). Each type will be 'C' or 'D', for credit or debit, respectively. Each amount will be an integer between 1 and 1000000, inclusive, with no leading zeros. You are to return an int representing the ending balance after processing all of the transactions.
Definition
Class: AccountBalance
Method: processTransactions
Parameters: int, String[]
Returns: int
Method signature: int processTransactions(int startingBalance, String[] transactions)
(be sure your method is public)
Constraints
- startingBalance will be between 0 and 1000000, inclusive.
- transactions will have between 0 and 50 elements, inclusive.
- Each element of transactions will be formatted as "type amount" (quotes added for clarity).
- Each type will be 'C' or 'D'.
- Each amount will represent an integer between 1 and 1000000, inclusive, with no leading zeros.
Examples
0)
100
{"C 1000", "D 500", "D 350"}
Returns: 250
This person had 100 dollars, got their paycheck, then went shopping at two different stores. 100 + 1000 - 500 - 350 = 250.
1)
100
{}
Returns: 100
With no transactions, the balance doesn't change by the end of the day.
2)
100
{"D 50", "D 20", "D 40"}
Returns: -10
Uh oh! This person's account is overdrawn.
3)
53874
{"D 1234", "C 987", "D 2345", "C 654", "D 6789", "D 34567"}
Returns: 10580
Problem Statement
You are working for the financial institution TopBank, and you have been tasked with writing a module that will take an initial account balance, along with a list of that day's transactions, and return the ending balance for the day.
Each transaction will be either a credit, which adds funds to the account, or a debit, which removes funds from the account. If a debit exceeds the available funds at the time, then the account balance will go negative. You will be given an int startingBalance, the initial account balance. You will also be given a String[] transactions, the list of transactions for the day. Each element of transactions will be in the form "type amount" (quotes added for clarity). Each type will be 'C' or 'D', for credit or debit, respectively. Each amount will be an integer between 1 and 1000000, inclusive, with no leading zeros. You are to return an int representing the ending balance after processing all of the transactions.
Definition
Class: AccountBalance
Method: processTransactions
Parameters: int, String[]
Returns: int
Method signature: int processTransactions(int startingBalance, String[] transactions)
(be sure your method is public)
Constraints
- startingBalance will be between 0 and 1000000, inclusive.
- transactions will have between 0 and 50 elements, inclusive.
- Each element of transactions will be formatted as "type amount" (quotes added for clarity).
- Each type will be 'C' or 'D'.
- Each amount will represent an integer between 1 and 1000000, inclusive, with no leading zeros.
Examples
0)
100
{"C 1000", "D 500", "D 350"}
Returns: 250
This person had 100 dollars, got their paycheck, then went shopping at two different stores. 100 + 1000 - 500 - 350 = 250.
1)
100
{}
Returns: 100
With no transactions, the balance doesn't change by the end of the day.
2)
100
{"D 50", "D 20", "D 40"}
Returns: -10
Uh oh! This person's account is overdrawn.
3)
53874
{"D 1234", "C 987", "D 2345", "C 654", "D 6789", "D 34567"}
Returns: 10580
public class AccountBalance {
public static int processTransactions(int startingBalance, String[] transactions) {
for (String trans : transactions) {
String param[] = trans.split(" ");
String TransType = param[0];
if (TransType.equalsIgnoreCase("C")) {
startingBalance = startingBalance + Integer.valueOf(param[1]);
}
else if (TransType.equalsIgnoreCase("D")) {
startingBalance = startingBalance - Integer.valueOf(param[1]);
}
}
return startingBalance;
}
}
*********************************************************************************