So I'm writing a program that mimics a phone keypad, whereas it would convert a string of text to integers: abc(2), def(3), ghi(4), jkl(5), mno(6), pqrs(7), tuv(8), wxyz(9). Except the output should have hyphens(-) between the digits.
Example input: Alabama
Output: 2-5-2-2-2-6-2
But my code only outputs 2522262. How would I go about formatting this correctly?
import java.util.Scanner;
public class PhoneKeypad {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(getNumbers(str));
}
private static final char[] DIGITS = (
// ABC DEF
"222" + "333"
+ // GHI JKL MNO
"444" + "555" + "666"
+ // PQRS TUV WXYZ
"7777" + "888" + "9999").toCharArray();
public static String getNumbers(CharSequence s) {
StringBuilder result = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
char c = Character.toUpperCase(s.charAt(i));
if ('A' <= c && c <= 'Z') {
result.append(DIGITS[c - 'A']);
}
}
}
Add the - after each digit. Easiest way I see, change
result.append(DIGITS[c - 'A']);
to
result.append(DIGITS[c - 'A']).append('-');
Then remove the last - when you return like,
public static String getNumbers(CharSequence s) {
StringBuilder result = new StringBuilder(s.length() * 2); // <-- digit-digit...
for (int i = 0; i < s.length(); i++) {
char c = Character.toUpperCase(s.charAt(i));
if ('A' <= c && c <= 'Z') {
result.append(DIGITS[c - 'A']).append('-');
}
}
if (result.length() > 1) {
result.setLength(result.length() - 1);
}
return result.toString(); // <-- Don't forget to return the result.
}
You might find it easier if you pass in s, you could call toUpperCase() and toCharArray() and then use a for-each loop. Like,
public static String getNumbers(String s) {
StringBuilder result = new StringBuilder(s.length() * 2);
for (char c : s.toUpperCase().toCharArray()) {
if (c >= 'A' && c <= 'Z') { // <-- I find this test easier to read,
// but that's just my opinion.
result.append(DIGITS[c - 'A']).append('-');
}
}
if (result.length() > 1) {
result.setLength(result.length() - 1);
}
return result.toString();
}
Related
I have this code which compresses characters in the given string and replaces repeated adjacent characters with their count.
Consider the following example:
Input:
aaabbccdsa
Expecting output:
a3b2c2dsa
My code is working properly but I think repeating if condition can be removed.
public class Solution {
public static String getCompressedString(String str) {
String result = "";
char anch = str.charAt(0);
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == anch) {
count++;
} else {
if (count == 1) { // from here
result += anch;
} else {
result += anch + Integer.toString(count);
} // to here
anch = ch;
count = 1;
}
if (i == str.length() - 1) {
if (count == 1) { // from here
result += anch;
} else {
result += anch + Integer.toString(count);
} // to here
}
}
return result;
}
}
In this solution code below is repeated two times
if (count == 1) {
result += anch;
} else {
result += anch + Integer.toString(count);
}
Please, note, I don't want to use a separate method for repeating logic.
You could do away with the if statements.
public static String getCompressedString(String str) {
char[] a = str.toCharArray();
StringBuilder sb = new StringBuilder();
for(int i=0,j=0; i<a.length; i=j){
for(j=i+1;j < a.length && a[i] == a[j]; j++);
sb.append(a[i]).append(j-i==1?"":j-i);
}
return sb.toString();
}
}
You can do something like this:
public static String getCompressedString(String str) {
String result = "";
int count = 1;
for (int i = 0; i < str.length(); i++) {
if (i + 1 < str.length() && str.charAt(i) == str.charAt(i + 1)) {
count++;
} else {
if (count == 1) {
result += str.charAt(i);
} else {
result += str.charAt(i) + "" + count;
count = 1;
}
}
}
return result;
}
I got rid of the repeated code, and it do as intended.
You can use this approach as explained below:
Code:
public class Test {
public static void main(String[] args) {
String s = "aaabbccdsaccbbaaadsa";
char[] strArray = s.toCharArray();
char ch0 = strArray[0];
int counter = 0;
StringBuilder sb = new StringBuilder();
for(int i=0;i<strArray.length;i++){
if(ch0 == strArray[i]){//check for consecutive characters and increment the counter
counter++;
} else { // when character changes while iterating
sb.append(ch0 + "" + (counter > 1 ? counter : ""));
counter = 1; // reset the counter to 1
ch0 = strArray[i]; // reset the ch0 with the current character
}
if(i == strArray.length-1){// case for last element of the string
sb.append(ch0 + "" + (counter > 1 ? counter : ""));
}
}
System.out.println(sb);
}
}
Sample Input/Output:
Input:: aaabbccdsaccbbaaadsa
Output:: a3b2c2dsac2b2a3dsa
Input:: abcdaaaaa
Output:: abcda5
Since, the body of the else and second if is the same, so we can merge them by updating the condition. The updated body of the function will be:
String result = "";
char anch = str.charAt(0);
int count = 0;
char ch = str.charAt(0); // declare ch outside the loop, and initialize to avoid error
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
if (ch == anch) {
count++;
}
// check if the second condition is false, or if we are at the end of the string
if (ch != anch || i == str.length() - 1) {
if (count == 1) { // from here
result += anch;
} else {
result += anch + Integer.toString(count);
} // to here
anch = ch;
count = 1;
}
}
// add the condition
// if count is greater than or
// if the last character added already to the result
if (count > 1 || (len < 2 || result.charAt(len - 2) != ch)) {
result += ch;
}
return result;
Test Cases:
I have tested the solution on the following inputs:
aaabbccdsa -> a3b2c2dsa
aaab -> a3b
aaa -> a3
ab -> ab
aabbc -> a2b2c
Optional
If you want to make it shorter, you can update these 2 conditions.
if (count == 1) { // from here
result += anch;
} else {
result += anch + Integer.toString(count);
} // to here
as
result += anch;
if (count != 1) { // from here
result += count;// no need to convert (implicit conversion)
} // to here
Here's a single-statement solution using Stream API and regular expressions:
public static final Pattern GROUP_OF_ONE_OR_MORE = Pattern.compile("(.)\\1*");
public static String getCompressedString(String str) {
return GROUP_OF_ONE_OR_MORE.matcher(str).results()
.map(MatchResult::group)
.map(s -> s.charAt(0) + (s.length() == 1 ? "" : String.valueOf(s.length())))
.collect(Collectors.joining());
}
main()
public static void main(String[] args) {
System.out.println(getCompressedString("aaabbccdsa"));
System.out.println(getCompressedString("awswwwhhhp"));
}
Output:
a3b2c2dsa // "aaabbccdsa"
awsw3h3p // "awswwwhhhp"
How does it work
A regular expression "(.)\\1*" is capturing a group (.) of identical characters of length 1 or greater. Where . - denotes any symbol, and \\1 is a back reference to the group.
Method Matcher.results() "returns a stream of match results for each subsequence of the input sequence that matches the pattern".
The only thing left is to evaluate the length of each group and transform it accordingly before collecting into the resulting String.
Links:
A quick tutorial on Regular Expressions.
Official tutorials on lambda expressions and streams
You can use a function which has the following 3 parameters : result, anch, count .
something of this sort:
private static String extractedFunction(String result,int count, char anch) {
return count ==1 ? (result + anch) : (result +anch+Integer.toString(count) );
}
make a function call from those two points like this :
result = extractedFunction(result,count,anch);
Try this.
static final Pattern PAT = Pattern.compile("(.)\\1*");
static String getCompressedString(String str) {
return PAT.matcher(str)
.replaceAll(m -> m.group(1)
+ (m.group().length() == 1 ? "" : m.group().length()));
}
Test cases:
#Test
public void testGetCompressedString() {
assertEquals("", getCompressedString(""));
assertEquals("a", getCompressedString("a"));
assertEquals("abc", getCompressedString("abc"));
assertEquals("abc3", getCompressedString("abccc"));
assertEquals("a3b2c2dsa", getCompressedString("aaabbccdsa"));
}
The regular expression "(.)\\1*" used here matches any sequence of identical characters. .replaceAll() takes a lambda expression as an argument, evaluates the lambda expression each time the pattern matches, and replaces the original string with the result.
The lambda expression is passed a Matcher object containing the results of the match. Here we are receiving this object in the variable m. m.group() returns the entire matched substring, m.group(1) returns its first character.
If the input string is "aaabbccdsa", it will be processed as follows.
m.group(1) m.group() returned by lambda
a aaa a3
b bb b2
c cc c2
d d d
s s s
a a a
I'm trying to figure out how to use a loop to append the delimiter ":" before the String s is found to contain a letter A-F.
If I have s = "10584f" then I would want the output to be "10584:f"
How should I go about doing this?
This is a simple solution that checks if it contains letters between a to f and put `:' accordingly.
public static void main(String[] args) {
String s = "10584f";
String newString = "";
for(int i=0;i<s.length();i++) {
if((int)s.charAt(i)>= (int)'a' && (int)s.charAt(i)<= (int)'f') {
newString = newString + ":" + s.charAt(i);
}else {
newString = newString + s.charAt(i);
}
}
System.out.println(newString);
}
You need to check for the ASCII code of each character and append the delimiter before it on this condition:
public class Main {
public static void main(String[] args) {
String s = "10584f";
System.out.println(appendDelimiter(s));
}
private static String appendDelimiter(String s) {
StringBuilder sb = new StringBuilder();
for(char c : s.toCharArray()) {
int charCode = (int) c;
if((charCode >= 65 && charCode <= 70) || (charCode >= 97 && charCode <= 102))
sb.append(":");
sb.append(c);
}
return sb.toString();
}
}
Output:
10584:f
If your target string consists of digits followed by a single letter from A-F or a-f, a simple solution can be: replacement of the regex-match, (\\d+)([A-Fa-f]). This regex means group(1) consisting of digits and group(2) consisting of a letter from A-F or a-f. The regex can be replaced with $1:$2 where $1 and $2 specify group(1) and group(2) respectively.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Test strings
String[] arr = { "10584f", "12345A", "13456b", "23456F" };
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i].replaceAll("(\\d+)([A-Fa-f])", "$1:$2");
}
// After replacement
System.out.println(Arrays.toString(arr));
}
}
Output:
[10584:f, 12345:A, 13456:b, 23456:F]
However, if the string can have multiple letters followed by digits and you want each letter to be prepended with a :, you can iterate the string starting from the index, 1 until the last character of the string and if you come across a letter, prepend it with a : as done in the function, withColonBeforeLetters given below:
Demo:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Test strings
String[] arr = { "10584f", "12345A", "13456b", "23456F", "1abc", "123aBc" };
for (int i = 0; i < arr.length; i++) {
arr[i] = withColonBeforeLetters(arr[i]);
}
// After replacement
System.out.println(Arrays.toString(arr));
}
static String withColonBeforeLetters(String s) {
StringBuilder sb = new StringBuilder();
// Put the first letter into sb
sb.append(s.charAt(0));
for (int i = 1; i < s.length(); i++) {
char ch = s.charAt(i);
if ((ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) {
sb.append(':').append(ch);
} else {
sb.append(ch);
}
}
return sb.toString();
}
}
Output:
[10584:f, 12345:A, 13456:b, 23456:F, 1:a:b:c, 123:a:B:c]
I'm a beginner at java and can't get this code to work. What I have to do is convert any inputted string to uppercase without using the toUpperCase string method. This is what I have:
public String toUpperCase(String str)
{
for(int i = 0; i < str.length(); i++)
{
char a = str.charAt(i);
a = Character.toUpperCase(a);
str += Character.toString(a);
}
return str;
}
You are using str as input, and output (so your String has infinite length, as you keep adding characters). And you can use static, because you aren't using instance state. And, you might use a for-each loop. Finally, add another String, or better a StringBuilder like
public static String toUpperCase(String str) {
StringBuilder sb = new StringBuilder();
for (char ch : str.toCharArray()) {
sb.append(Character.toUpperCase(ch));
}
return sb.toString();
}
There is the following way, but it doesn't consider any characters outside of English (no diacritics, no other language’s characters behind a-z).
public String toUpperCase(String str) {
char[] chars = str.toCharArray();
for (int i=0; i<chars.length; i++) {
char c = chars[i];
if ('a' <= c && c <= 'z') {
chars[i] = (char) (c - 'a' + 'A');
}
}
return new String(chars);
}
I am aware your school probably do not allow you to use StringBuilder and in case you can't use array as well. This is another primitive approach which your school may accept:
public static String toUpperCase(String s){
String str = "";
for(int x=0; x<s.length(); x++){
char ch = s.charAt(x);
if(ch >= 'a' && ch <= 'z')
str += "" + (char)(ch - 32);
else
str += "" + ch;
}
return str;
}
Test:
System.out.println(toUpperCase("aAbBcC"));
Output:
AABBCC
Since you can't use the toUpperCase() method, you can use the ASCII table to get from a lower case letter to an upper case letter by subtracting 32.
'a' = 97, 'A' = 65
'b' = 98, 'B' = 66
...
'z' = 122, 'Z' = 90
public static int DIFF = 'a' - 'A'; // 32
public static String toUpperCase(String str) {
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray()) {
if (Character.isLowerCase(c)) {
sb.append(String.valueOf((char)(c - DIFF)));
} else {
sb.append(c);
}
}
return sb.toString();
}
try it:
public static String toUpperCase(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
int v = str.charAt(i);
if (v > 96 && v < 123) {
v -= 32;
}
result+=(char)v;
}
return result;
}
C'mon guys, Java 8 has been out for years!
/**
* Converts an all-lowercase String to
* uppercase. Retains only spaces, any
* other characters will be lost.
*/
public static String toUpperCase(String s) {
int diff = 'a' - 'A'; // 32
return s.chars()
.filter(c -> c >= 'a' && c <= 'z' || c == ' ')
.mapToObj(c -> String.valueOf((char) (c - (diff))))
.collect(Collectors.joining());
}
I have this code that is supposed to do what the title said, reverse the order of characters without changing the order of the words:
package stackTests;
import java.util.Scanner;
import java.util.Stack;
public class StackTest
{
Stack<Character> stack;
public StackTest()
{
stack = new Stack<Character>();
}
public String reverseString(String str)
{
int start = 0;
int start2 = 0;
int size;
char space = ' ';
char[] cArr;
Scanner scan = new Scanner(str);
cArr = str.toCharArray();
for (; start < cArr.length; start++)
{
if(cArr[start] == space || start == cArr.length - 1)
{
for (; start2 < stack.size(); start++)
{
System.out.print(stack.pop());
}
}
else
{
stack.push(cArr[start]);
}
start2 = 0;
}
return str;
}
}
It works fine if I enter a single word like "Hello"--it will output "olleH"--but as soon as it gets more complicated than one word it starts to output some weird things."Hello my name is" outputs "ollehem". I'm really new to Stacks and this is my first time using them. I'm not sure if there is a logic error or improper use of Stacks.
you're not outputting original spaces, this is why you're seeing strange results
here is fixed version:
public static void reverseString(final String str) {
final Stack<Character> stack = new Stack<>();
for (int i = 0; i < str.length(); i++) {
final char c = str.charAt(i);
if (c == ' ') {
while (!stack.isEmpty())
System.out.print(stack.pop());
System.out.print(' ');
} else
stack.push(c);
}
while (!stack.isEmpty())
System.out.print(stack.pop());
}
another version without stack, with in-place replacement:
public static void reverseString(final String str) {
final char[] chars = str.toCharArray();
int start = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ' ') {
reverse(chars, start, i - 1);
start = i + 1;
}
}
reverse(chars, start, chars.length - 1);
System.out.println(new String(chars));
}
private static void reverse(final char[] chars, int s, int e) {
while (s < e) {
final char t = chars[s];
chars[s] = chars[e];
chars[e] = t;
s++;
e--;
}
}
If you HAVE to use a stack, I would follow an algorithm like this:
String myString = "Hello World";
Stack<Character> stack = new Stack<Character>();
StringBuilder sb = new StringBuilder();
String[] splitString = myString.split(" ");
//Iterate through each word in the string
for(String s : splitString){
//Push each character of the word into LIFO stack
for(char c : s.toCharArray()){
stack.push(c);
}
//Build new string with reverse ordered characters
while(!stack.isEmpty()){
sb.append(stack.pop());
}
//Append a space as long as it's not the last word of the original string
if(!s.equals(splitString[splitString.length - 1]))
sb.append(" ");
}
//Print the new string
System.out.println(sb.toString());
I'm not sure efficiency matters to you, but this algorithm would work in linear time, where n is the number of characters in the string.
Here is how you can do it in-place without using any extra space (Not using stack):
public class ReverseWordsInplace {
public static void main(String[] args) {
reverseWords(new StringBuilder("This is a test"));
}
public static void reverseWords(StringBuilder s) {
StringBuilder str = new StringBuilder(s);
int startWordIndex = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ' || str.length() - 1 == i) {
int x = 0;
int endWordIndex = str.charAt(i) == ' ' ? i - 1 : i;
while (endWordIndex - x > startWordIndex + x) {
char c1 = str.charAt(startWordIndex + x);
char c2 = str.charAt(endWordIndex - x);
str.setCharAt(startWordIndex + x, c2);
str.setCharAt(endWordIndex - x, c1);
x++;
}
startWordIndex = i + 1;
}
}
System.out.println(str);
}
}
Output:
sihT si a tset
I've been trying to use an array but it just seems to return the original String.
public static String capitalizeEveryOtherWord(String x) {
x = x.toLowerCase();
x.trim();
String[] words = x.split(" ");
for(int c = 2; c < words.length; c += 2)
words[c].toUpperCase();
return Arrays.toString(words);
}
Could anyone help?
toUpperCase() and trim() return new strings instead of modifying existing ones. You need to assign those new strings to something.
public static String capitalizeEveryOtherWord(String x) {
x = x.toLowerCase();
x = x.trim();
String[] words = x.split(" ");
for (int c = 2; c < words.length; c += 2)
words[c] = words[c].toUpperCase();
return Arrays.toString(words);
}
Also, you probably meant to start at index 0 or 1 – the first or second element, respectively.
Minitech has correctly identified the problem IMHO, but I would use a different regex-based approach:
public static String capitalizeEveryOtherWord(String x) {
StringBuilder result = new StringBuilder(x);
Matcher matcher = Pattern.compile("^ *\\w|\\w* \\w+ \\w").matcher(x);
while(matcher.find())
result.setCharAt(matcher.end() - 1, Character.toUpperCase(x.charAt(matcher.end() - 1)));
return result.toString();
}
(Tested and works).
This also works:
public class answerForStackOverflow {
public static void main(String[] args) {
String examplestring = "iwouldreallyliketothankforallthehelponstackoverflow";
String output = "";
for (int i = 0; i < examplestring.length(); i++) {
char c = examplestring.charAt(i);
if (i % 2 == 0) {
output += examplestring.substring(i, i + 1).toUpperCase();
} else {
output += examplestring.substring(i, i + 1);
}
}
System.out.println(output);
}
}