Replace all instances of a character in a String - java

I'm trying to create a method that replace all instances of a certain character in a word with a new character. This is what I have so far:
public class practice {
public static void main(String[] args) {
String test3 = updatePartialword("----", "test", 't');
System.out.println(test3); }
public static String updatePartialword(String partial, String secret, char c) {
String newPartial = "";
int len = secret.length();
for (int i=0; i<=secret.length()-1; i++){
char x = secret.charAt(i);
if (c==x) {
String first = partial.substring(0,i);
String second = partial.substring(i+1,len);
newPartial = first+x+second;
}
}
return newPartial;
}
}
I want it to return t--t, but it will only print the last t. Any help would be greatly appreciated!

Java already has a built in method in String for this. You can use the the replace() method to replace all occurrences of the given character in the String with the another character
String str = "Hello";
str.replace('l', '-'); //Returns He--o
str.replace('H', '-'); //Returns -ello

I suspect you are looking for something like
public static void main(String[] args) {
String test3 = updatePartialword("----", "test", 't');
System.out.println(test3);
}
public static String updatePartialword(String partial, String secret, char c) {
char[] tmp = partial.toCharArray();
for (int i = 0; i < secret.length(); i++) {
char x = secret.charAt(i);
if (c == x) {
tmp[i] = c;
}
}
return new String(tmp);
}

In your code you overwrite the String each time you found the character. Instead of overwriting, you should expand the string each time.
public class practice {
public static void main(String[] args) {
String test3 = updatePartialword("----", "test", 't');
System.out.println(test3);
}
public static String updatePartialword(String partial, String secret, char c) {
StringBuilder sb = new Stringbuilder();
sb.append(""); // to prevent the Stringbuilder from calculating with the chars
for (int i = 0; i < partial.lenght; i++)
if (secret.charAt(i) == c)
sb.append(c);
else
sb.append('-');
return sb.toString();
}
}

Related

Convert uppercase string to lowercase using charAt

I was wondering what is exactly wrong with the following code. I'm getting error on the line after the if statement. This code takes a string with both uppercase and lowercase letters but returns the string after converting the uppercase letters to lowercase.
public class Main {
public static void main(String[] args) {
toLowerCase("HeLloWoRlD!");
}
private static String toLowerCase(String str) {
for (int i = 0; i < str.length(); i++) {
if (Character.isUpperCase(str.charAt(i))) {
str.charAt(i) = Character.toLowerCase(str.charAt(i));
}
}
return str;
}
}
str.charAt(i) cannot be the left hand side of an assignment operator. It's a value returned by a method call, you can't assign to it.
Besides, Strings are immutable. You cannot modify the characters of str.
You'll have to create a new String for your method to return.
For example:
private static String toLowerCase(String str) {
StringBuilder sb = new StringBuilder(str.length());
for (int i = 0; i < str.length(); i++) {
if (Character.isUpperCase(str.charAt(i))) {
sb.append (Character.toLowerCase(str.charAt(i)));
} else {
sb.append (str.charAt(i));
}
}
return sb.toString();
}
Change you code to the following :
private static String toLowerCase(String str) {
StringBuffer lower = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (Character.isUpperCase(str.charAt(i))) {
lower.append(Character.toLowerCase(str.charAt(i)));
} else {
lower.append(str.charAt(i));
}
}
return lower.toString();
}
public static void main(String[] args) throws IOException {
System.out.println(toLowerCase("HeLloWoRlD!"));
}
A string is immutable so you can't change the existing one on the fly. Instead you can create a StringBuffer and append the values accordingly as you iterate over the original str.
String is immutable so you cannot change (reassign) the characters inside the string.
Here is the simplest solution, just using the built in method in String class:
private static String toLowerCase(String str) {
return str == null ? null : str.toLowerCase(); //consider str.toLowerCase(Locale.ROOT) if you are using non english language with special characters
}
You are making use of the String.charAt(i) which returns character Value at that position. It is not the reference location that you can assign a value to.
Please check the below documentation.
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#charAt-int-
You need to convert to a character Array the string if you want to modify it using the charAt feature
private static String toLowerCase(String str) {
char[] newStr = str.toCharArray();
for (int i = 0; i < str.length(); i++) {
if (Character.isUpperCase(str.charAt(i))) {
newStr[i] = Character.toLowerCase(str.charAt(i));
}
}
return new String(newStr);
}
The method charAt(i) is used for getting character by index(position) in the string. However, You used it for setting, what is not right.
So, in your case, method toLowerCase() should return new String object, for example.
private static String toLowerCase(String str) {
String returning_Str = "";
for (int i = 0; i < str.length(); i++) {
char test_char = str.charAt(i);
if (Character.isUpperCase(test_char)) {
test_char = Character.toLowerCase(test_char);
}
returning_Str += test_char;
}
return returning_Str;
}
You cannot change the content of a String. You have to create a new object instead:
public class Main {
public static void main(String[] args) {
toLowerCase("HeLloWoRlD!");
}
private static String toLowerCase(String str) {
for (int i = 0; i < str.length(); i++) {
if (Character.isUpperCase(str.charAt(i))) {
str = new String(str.replace(str.charAt(i),Character.toLowerCase(str.charAt(i))));
}
}
return str;
}
}
First take a variable the assign value on it.
This code will work as expected
public class Main {
public static void main(String[] args) {
System.out.println(toLowerCase("HeLloWoRlD!"));
}
private static StringBuffer toLowerCase(String str) {
StringBuffer buf=new StringBuffer();
for (int i = 0; i < str.length(); i++) {
char c=str.charAt(i);
if (Character.isUpperCase(str.charAt(i))) {
c = Character.toLowerCase(str.charAt(i));
buf.append(c);
}else{
buf.append(c);
}
}
return buf;
}
}
private static String toLowerCase(String str){
return str.chars().map(Character::toLowerCase).collect(StringBuilder::new,
StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
or
private static String toLowerCase(String str){
return str.toLowerCase();
}

Java: find common characters in two strings

I'm asked to write a program that finds the common characters in two strings using the indexOf(char) method and a for loop. Here's what I have so far - the output comes out blank still.
import java.util.Scanner;
public class ClassName {
public static void main (String args []) {
Scanner input = new Scanner (System.in);
String a = "";
String b = "";
String c = "";
System.out.print("Enter two words: ")
a = input.nextLine();
b = input.nextLine();
for (int i = 0; i < a; i++){
char ch = a.charAt(i);
if (b.indexOf(ch) != -1){
c = c+String.valueOf(ch);
}
}
System.out.print("Common letters are: "+c);
}
}
output here
I'm not sure where to go from here.
thanks
Your code will duplicate common characters for example if you compare "developper" to "programmer" your result string will contain three time the e character
If you don't want that behaviour I suggest that you also use a Set like this:
public class CommonCharsFinder {
static String findCommonChars(String a, String b) {
StringBuilder resultBuilder = new StringBuilder();
Set<Character> charsMap = new HashSet<Character>();
for (int i = 0; i < a.length(); i++) {
char ch = a.charAt(i); //a and b are the two words given by the user
if (b.indexOf(ch) != -1){
charsMap.add(Character.valueOf(ch));
}
}
Iterator<Character> charsIterator = charsMap.iterator();
while(charsIterator.hasNext()) {
resultBuilder.append(charsIterator.next().charValue());
}
return resultBuilder.toString();
}
// An illustration here
public static void main(String[] args) {
String s1 = "developper";
String s2 = "programmer";
String commons = findCommonChars(s1, s2);
System.out.println(commons);
}
}
Result from the example:
public Set<Character> commonChars(String s1, String s2) {
Set<Character> set = new HashSet<>();
for(Character c : s1.toCharArray()) {
if(s2.indexOf(c) >= 0) {
set.add(c);
}
}
return set;
}
public class CommonCharFromTwoString {
public static void main(String args[])
{
System.out.println("Enter Your String 1: ");
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
System.out.println("Enter Your String 2: ");
String str2 = sc.nextLine();
Set<String> str = new HashSet<String>();
for(int i=0;i<str1.length();i++){
for (int j = 0; j < str2.length(); j++) {
if(str1.charAt(i) == str2.charAt(j)){
str.add(str1.charAt(i)+"");
}
}
}
System.out.println(str);
}
}

Using StringBuilder getting null as output

I am doing one coding question in which I try to decrypt the input string. The procedure for the decryption is:
from 0 to 9 it represent alphabets from a to i.
then 10# represent j, 11# represent k and so.
import java.util.HashMap;
public class Julia {
public static void main(String[] args) {
String s="10#21#12#91";
Julia obj=new Julia();
String result=obj.decrypt(s);
System.out.println(result);
}
public String decrypt(String msg)
{
HashMap<String,Character> hs=new HashMap<>();
hs.put("1",'a');
hs.put("2",'b');
hs.put("3",'c');
hs.put("4",'d');
hs.put("5",'e');
hs.put("6",'f');
hs.put("7",'g');
hs.put("8",'h');
hs.put("9",'i');
hs.put("10",'j');
hs.put("11",'k');
hs.put("12",'l');
hs.put("13",'m');
hs.put("14",'n');
hs.put("15",'o');
hs.put("16",'p');
hs.put("17",'q');
hs.put("18",'r');
hs.put("19",'s');
hs.put("20",'t');
hs.put("21",'u');
hs.put("22",'v');
hs.put("23",'w');
hs.put("24",'x');
hs.put("25",'y');
hs.put("26",'x');
StringBuilder n=new StringBuilder();
for(int i=msg.length()-1;i>=0;i--)
{
if(msg.charAt(i)=='#' && i>=2)
{
StringBuilder s=new StringBuilder().append(msg.charAt(i-2)).append(msg.charAt(i-1));
System.out.println(s);
n.append(hs.get(s));
System.out.println(n);
i=i-2;
}
else
{
n.append(hs.get(msg.charAt(i)));
}
}
return n.toString();
}
}
That is code I wrote. But the output I am getting is nullnullnullnullnull.
I think the issue is with StringBuilder. Can anyone help me with that and explain the concept? If someone has better solution please guide.
You should not use data (a map) when you could have used a simple formula.
My suggestion:
import java.util.ArrayList;
import java.util.List;
public final class Julia {
public static void main(final String[] args) {
final String s = "10#21#12#91";
final String result = decrypt(s);
System.out.println(result);
}
private static String decrypt(final String s) {
final List<Integer> crypt = new ArrayList<>();
final String[] groups = s.split("#");
for (int i = 0; i < groups.length; i++) {
final String group = groups[i];
int j = 0;
// Special case for last group
if ((i == (groups.length - 1)) && !s.endsWith("#")) {
j = group.length();
}
if (group.length() > 2) {
j = group.length() - 2;
}
for (int k = 0; k < j; k++) {
crypt.add(Integer.valueOf(group.substring(k, k + 1)));
}
if (j < group.length()) {
crypt.add(Integer.valueOf(group.substring(j, group.length())));
}
}
final StringBuilder n = new StringBuilder(crypt.size());
for (final Integer c : crypt) {
final char d = (char) (('a' + c) - 1);
n.append(d);
}
return n.toString();
}
}
Please note that there are two mistakes in the question: The letter a is 1, not zero, and the value for 26 is z, not x. The latter error is typical when you use data where a formula would do.
Since you are learning, I would note that the decrypt methods - both my suggestion and yours - should be static since they do not use any fields, so the instantiation is not necessary.
This is Pattern Matching problem which can be solved by Regex.
Your code has some bugs and those are already pointed out by others. I don't see any solution which looks better than a simple regex solution.
Below regex code will output 'julia' for input '10#21#12#91'.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Julia {
public static void main(String[] args) {
String s="10#21#12#91";
Julia obj=new Julia();
String result=obj.decrypt(s);
System.out.println(result);
}
public String decrypt(String msg)
{
Pattern regex = Pattern.compile("((\\d\\d#)|(\\d))");
Matcher regexMatcher = regex.matcher(msg);
StringBuffer result = new StringBuffer();
while (regexMatcher.find())
regexMatcher.appendReplacement(result, getCharForNumber(Integer.parseInt(regexMatcher.group(1).replace("#",""))));
return result.toString();
}
private String getCharForNumber(int i) {
return i > 0 && i < 27 ? String.valueOf((char)(i + 96)) : null;
}
}
I hope it helps.
hs.get(s) will always return null, since s is not a String.
Try hs.get(s.toString())
hs.get(msg.charAt(i)) will also always return null, since you are passing a char to get instead of String.
There may also be logic problems in your code, but it's hard to tell.
Optimized version of your code
public class Main {
public static void main(String[] args) {
String cipher = "10#21#12#91";
System.out.print(decrypt(cipher));
//output : julia
}
static String decrypt(String cipher) {
//split with # to obtain array of code in string array
String[] cipher_char_codes = cipher.split("#");
//create empty message
StringBuilder message = new StringBuilder();
//loop for each code
for (String code : cipher_char_codes) {
//get index of character
int index = Integer.parseInt(code);
if (index > 26) {
char[] pair = code.toCharArray();
for (int i = 0; i < pair.length; i++) {
int x = Integer.parseInt("" + code.charAt(i));
message.append((char) ('a' + ((x - 1) % 26)));
}
} else {
//map index into 1 to 26
//find ascii code and cast into char
message.append((char) ('a' + ((index - 1) % 26)));
}
}
return message.toString();
}
}
Regex is indeed the way to go, and the code proposed by Pirate_Jack can be improved. It calls the expensive regex two superfluous times (replace is a regex operation).
Following is a yet improved version:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class Julia3 {
public static void main(final String[] args) {
final String s = "10#21#12#91";
final String result = decrypt(s);
System.out.println(result);
}
public static String decrypt(final String msg) {
final Pattern regex = Pattern.compile("((\\d\\d)(#)|(\\d))");
final Matcher regexMatcher = regex.matcher(msg);
final StringBuffer result = new StringBuffer();
String c;
while (regexMatcher.find()) {
if (regexMatcher.group(2) == null) {
c = regexMatcher.group(1);
} else {
c = regexMatcher.group(2);
}
result.append((char) ((Integer.parseInt(c) + 'a') - 1));
}
return result.toString();
}
}
This is not right :
hs.get(s)
s is a StringBuilder. It should be hs.get(Char)
Edit: an optional different solution:
public class Julia {
public static void main(String[] args) {
String s="10#21#12#91";
List<String> numbers = splitToNumbers(s);
Julia obj=new Julia();
String result=obj.decrypt(numbers);
System.out.println(result);
}
/**
*#param s
*#return
*/
private static List<String> splitToNumbers(String s) {
//add check s is not null
char[] chars = s.toCharArray();
char delimiter = '#';
List<String> numberAsStrings = new ArrayList<String>();
int charIndex = 0;
while (charIndex < (chars.length -3)) {
char theirdChar = chars[charIndex+2];
if(theirdChar == delimiter) {
numberAsStrings.add(""+chars[charIndex]+chars[charIndex+1]);
charIndex +=3;
}else {
numberAsStrings.add(""+chars[charIndex]);
charIndex ++;
}
}
//add what's left
while (charIndex < chars.length) {
numberAsStrings.add(""+chars[charIndex]);
charIndex++;
}
return numberAsStrings;
}
public String decrypt(List<String> numbersAsStings){
StringBuilder sb=new StringBuilder();
for (String number : numbersAsStings) {
int num = Integer.valueOf(number);
sb.append(intToChar(num-1));
}
return sb.toString();
}
private char intToChar(int num) {
if((num<0) || (num>25) ) {
return '?' ;
}
return (char)('a' + num);
}
}

Convert String to its Unicode code point

Assuming I have a string foo = "This is an apple"
The Unicode code point equivalent will be
" \\x74\\x68\\x69\\x73.......... \\x61\\x70\\x70\\x6c\\x65 "
T h i s ............. a p p l e
How do I convert from String foo
to
String " \\x74\\x68\\x69\\x73.......... \\x61\\x70\\x70\\x6c\\x65 "
try this..
public static String generateUnicode(String input) {
StringBuilder b = new StringBuilder(input.length());
for (char c : input.toCharArray()) {
b.append(String.format("\\u%04x", (int) c));
}
return b.toString();
}
Here a working code snippet to make the conversion:
public class HexTest {
public static void main(String[] args) {
String testStr = "hello日本語 ";
System.out.println(stringToUnicode3Representation(testStr));
}
private static String stringToUnicode3Representation(String str) {
StringBuilder result = new StringBuilder();
char[] charArr = str.toCharArray();
for (int i = 0; i < charArr.length; i++) {
result.append("\\u").append(Integer.toHexString(charArr[i] | 0x10000).substring(1));
}
return result.toString();
}
}
That display:
\u0068\u0065\u006c\u006c\u006f\u65e5\u672c\u8a9e\u0020
If you want to get rid of the extra zeros you elaborate it as described here.
Here another version to do the conversion, by passing "This is an apple" you get
\u54\u68\u69\u73\u20\u69\u73\u20\u61\u6e\u20\u61\u70\u70\u6c\u65
by using:
private static String str2UnicodeRepresentation(String str) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
int cp = Character.codePointAt(str, i);
int charCount = Character.charCount(cp);
//UTF characters may use more than 1 char to be represented
if (charCount == 2) {
i++;
}
result.append(String.format("\\u%x", cp));
}
return result.toString();
}

Counting the spaces in a string

I want to count the spaces in a string:
public class SongApp {
public static void main(String[] args) {
String word = "a b c";
int i =0,spaceCount=0;
while(i<word.length()){
char temp = word.charAt(i);
System.out.println(temp);
if(" ".equals(temp)){
spaceCount++;
}
i++;
}
System.out.println("Spaces in string: "+spaceCount);
}
}
When I replace the if statement with if(temp.equals(" ")), I get a "cannot invoke(String) on the primitive type char.
I don't understand why this won't work.
It won't work because you are calling a method of Class String (equals()) on a value which is of primitive type 'char'. You are trying to compare a 'char' with a 'String'.
You must compare between 'char's and since it's a primitive value you need to use '==' boolean compare operator like:
public class SongApp {
public static void main(String[] args) {
String word = "a b c";
int i = 0,
spaceCount = 0;
while( i < word.length() ){
if( word.charAt(i) == ' ' ) {
spaceCount++;
}
i++;
}
System.out.println("Spaces in string: "+spaceCount);
}
}
You can use the replace function for String to replace all the spaces(" ") with no spaces("") and get the difference between the lengths before and after calling the replace function.
Go through this example:
class Test{
public static void main(String args[]){
String s1 = "a b c";
int s1_length = s1.length();
System.out.println(s1_length); // 5
String s2 = s1.replace(" ","");
int s2_length = s2.length();
System.out.println(s2_length); // 3
System.out.println("No of spaces = " + (s1_length-s2_length)); // No of spaces = 2
}
}
You can use commons-lang.jar to calculate this.
`public class Main {
public static void main(String[] args) {
String word = "a b c";
System.out.println("Spaces in string: " + StringUtils.countMatches(word," "));
}
}`
The source of "StringUtils.countMatches" is below:
public static int countMatches(String str, String sub) {
if (isEmpty(str) || isEmpty(sub)) {
return 0;
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != INDEX_NOT_FOUND) {
count++;
idx += sub.length();
}
return count;
}
public class CountSpace {
public static void main(String[] args) {
String word = "a b c";
String data[];int k=0;
data=word.split("");
for(int i=0;i<data.length;i++){
if(data[i].equals(" ")){
k++;
}
}
System.out.println(k);
}
}

Categories

Resources