For 1), here's a very straightforward regex way:
String in = "helllo goodbye";
String out = in.replaceAll("(.)(?=\\1)", "$1*");
System.out.println(out);
Prints:
hel*l*lo go*odbye
Explanation:
(.) //match any one character into group 1
(?=\\1) //positive lookahead for that same character by backreferencing group 1
$1* //replace that one character with the character followed by *
I might take a crack at 2) later, but I don't like multiple questions wrapped into one.
Edit
Alright since I'm in a regex mood here's 2):
String in = "xhixhix";
String out = in;
while (!out.matches("[^x]*x*")) {
out = out.replaceAll("x(.*)", "$1x");
}
System.out.println(out);
This replaces x(something) with (something)x until all the xs are at the end of the string. I'm sure that there's a better way of doing this one with/than regex though.
String Manipulation Problems in Java - Stack Overflow
bufferedreader - Java String Program - Stack Overflow
manipulating strings in Java
What Every Java Developer should know about String
For 1), here's a very straightforward regex way:
String in = "helllo goodbye";
String out = in.replaceAll("(.)(?=\\1)", "$1*");
System.out.println(out);
Prints:
hel*l*lo go*odbye
Explanation:
(.) //match any one character into group 1
(?=\\1) //positive lookahead for that same character by backreferencing group 1
$1* //replace that one character with the character followed by *
I might take a crack at 2) later, but I don't like multiple questions wrapped into one.
Edit
Alright since I'm in a regex mood here's 2):
String in = "xhixhix";
String out = in;
while (!out.matches("[^x]*x*")) {
out = out.replaceAll("x(.*)", "$1x");
}
System.out.println(out);
This replaces x(something) with (something)x until all the xs are at the end of the string. I'm sure that there's a better way of doing this one with/than regex though.
In the first case you can iterate through the string keeping track of the previous character and comparing it to the current. Use a StringBuilder to build the new String.
In the second, iterate through, using two string builders (one for characters other than x, one for x), combine them at the end.
This is just off the top of my head in 30 seconds, and is the simplest approach. The second thought is that I'd prob use a regular expression, now that I think about it for 10 more seconds :)
char d= c-32;
c-32 expression results in int (because 32 will be treated as int) and you are trying to assign int value to type char, which is why you are getting "possible loss of precision error". You need to explicitly cast 32 to char (or) entire expression on right side to char.
Same rule applies for char d=c+32; statement also and other places you have used int value with char type.
char d = (char) (c-32); // note the parentheses
char d = (char) (c+32); // around (char + int); see below
32is anintliteral. The expression resulting from (char+int) arithmetic is of typeintas well. Since, the range of acharis a subset ofint, compiler complains about the potential loss of precision. By explicitly casting to(char), you're effectively telling the compiler that you know what you're doing.Note, the parentheses around the arithmetic expression
(c+32). Without it the compiler evaluates this as(char)(c) + 32which again results in anintdue to the higher precedence of casting.
So, I'm currently in week 5 of my intro to programming class, and I just noticed that when it comes to manipulating Strings, chars, or just any text in general, it's very difficult for me. I'm not having any issues figuring out loops and what not when it comes to numbers, but strings have been very very difficult for me. I was just working on a problem trying to create a for loop that took the users imputed word and added the same word onto itself x number of times, x being the amount of characters in the users word. I've figured it out with the help of a tutor, but he showed me how to do it my way and also showed me how he would have done it. They were two different ways of setting up the for loop which I get there's a million different ways to do stuff in Java, I'm just having a very hard time manipulating text and haven't had any issues manipulating numbers. Has anyone else been through this same situation? Maybe I'm making it harder on myself? I also know lots of practice is the key to coding so I'm doing all the practicing I can. Just looking for opinions on what I should focus on or maybe a thought process that would help? Thanks.
Edit: Also, are there any specific things I should study or career paths that I should research that mainly deal with numbers when coding?