For example:
You're given a word and Set of letters. If the word contains letters that are not within the Set, then those letters are "replaced" with dashes. You're not actually supposed to change the given word, but create a new String that reflects those changes.
Say you were given a word: "magikarp". The Set contains the letters 'm', 'k', 'p'. The String that you would return would be "m---k--p".
How would you accomplish this by utilizing only a String and String methods? I also can't use any external libraries.
It is more intuitive to me to use arrays, but this has to be performed by building up a String instead of building any extra data structures for the sake of efficiency.
This is how I approached it (working solution but not done by building up a String), for further clarification:
public String getPattern (SortedSet<Character> guesses, String word) {
char[] pattern = word.toCharArray();
for (int i = 0; i < wordLength; i++) {
if (!guesses.contains(pattern[i])) {
pattern[i] = '-';
}
}
// Pads each character in the generated String with spaces and trims
// the leading and trailing spaces.
return new String(pattern).replace("", "").trim();
}
}
You can just use what String already provides, a method to get each character and a method to replace a specifc character with another:
public String getPattern (Set<Character> guesses, String word) {
for (int i=0; i<word.length(); ++i) {
char c = word.charAt(i);
if (!guesses.contains(c))
word = word.replace(c, '-');
}
return word;
}
Its not very efficient because it will create a new string instance for every character that needs to be replaced. For efficiency using a StringBuilder would be better.
How about building a regex String from the Set of characters, and using that as a param to the String.replaceAll method to return the filtered String?
Set<Character> letters = new HashSet<>();
letters.add('m');
letters.add('k');
letters.add('p');
String filter = "[^(";
for (Character letter : letters) {
filter += letter;
}
filter += ")]";
String toBeReplaced = "magikarp";
String result = toBeReplaced.replaceAll(filter, "-");
This is most certainly not the best way to approach the problem, except that it "builds up the String".
String newWord = "";
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
newWord = newWord + (
guesses.contains(c) ? c : '-'
);
}
This approach creates a new String instance on each letter rather than doing a quick replace. But it "builds it up".
Related
I'm a programming newbie and I am doing a coderbyte exercise that says "
Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a)"
i'm thinking of the following methods:
declare a string called "abcdefghijklmnopqrstuvxyz" and compare each string's char index position with the alphabet's index position, and then just bring the alphabet char that is located at the i+1 index location. But I don't know how it would work from z to a.
I've seen some techniques using ASCII values for every char but I've never done that before and not sure how it works
convert the given string into a char[] array, but then I'm not sure how I would tell the system to get me the next alphabet char
What would be the easiest way to do this?
EDIT
this is my code so far, but it doesn't work.
import java.util.*;
import java.io.*;
class Main {
public static String LetterChanges(String str) {
// code goes here
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String newWord = "";
for (int i = 0; i < str.length(); i++){
for (int j = 0; j < alphabet.length(); i++){
if (str[i] == alphabet[i]){
if (alphabet[i+1].isVowel()){
newWord = newWord + toUpperCase(alphabet[i+1]);
}
else{
newWord = newWord + alphabet[i+1];
}
}
}
}
return str;
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(LetterChanges(s.nextLine()));
}
}
Can't I ask for the index position of a Char that is a part of a String? in C I could do that.
Other than that not sure why it doesn't work.
I would definitely go with method 1.
I believe what you're looking for is the indexOf method on a String.
First of, I would create a method that given a character finds the next letter in the alphabet and return that. This could be done by finding the letter in your alphabet string and then fetch the letter at index+1. As you also pointed out you would need to take care of the edge case to turn 'z' into 'a', could by done with an if-statement or by having an extra letter 'a' at the end of your alphabet string.
Now all that remains to do is create a loop that runs over all characters in the message and calls the previously made method on that character and constuct a new string with the output.
Hope this helps you figure out a solution.
Assuming that there would be only lower case English letters in the given String the most performant way would be to add +1 to every character, and use either if-statement checking whethe the initial character was z or use the modulo operator % as #sp00m has pointed out in the comment.
Performing a search in the alphabetic string (option 1 in your list) is redundant, as well extracting array char[] from the given string (option 3).
Checking the edge case:
public static String shiftLetters(String str) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char next = str.charAt(i);
if (next == 'z') result.append('a'); // checking the edge case
else result.append((char) (next + 1));
}
return result.toString();
}
Applying modulo operator:
public static String shiftLetters(String str) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char next = (char) ((str.charAt(i) - 'a' + 1) % 26 + 'a');
result.append(next);
}
return result.toString();
}
main()
public static void main(String[] args) {
System.out.println(shiftLetters("abc"));
System.out.println(shiftLetters("wxyz"));
}
Output:
bcd // "abc"
xyza // "wxyz"
I need to replace all commas after the 5th one. So if a String contains 10 commans, I want to leave only the first 5, and remove all subsequent commas.
How can I do this ?
String sentence = "Test,test,test,test,test,test,test,test";
String newSentence = sentence.replaceAll(",[6]","");
Just capture all the characters from the start upto the 5th comma and match all the remaining commas using the alternation operator |. So , after | should match all the remaining commas. By replacing all the matched chars with $1 will give you the desired output.
sentence.replaceAll("^((?:[^,]*,){5})|,", "$1");
DEMO
In case you were wondering how to solve this problem without using regular expressions... There are libraries that could make your life easier but here is the first thought that came to mind.
public String replaceSpecificCharAfter( String input, char find, int deleteAfter){
char[] inputArray = input.toCharArray();
String output = "";
int count = 0;
for(int i=0; i <inputArray.length; i++){
char letter = inputArray[i];
if(letter == find){
count++;
if (count <= deleteAfter){
output += letter;
}
}
else{
output += letter;
}
}
return output;
}
Then you would invoke the function like so:
String sentence = "Test,test,test,test,test,test,test,test";
String newSentence = replaceSpecificCharAfter(sentence, ',', 6);
I have look into most of questions but I couldn't find how to uppercase or lowercase specific character inside a word.
Example:
String name = "Robert"
What if I would like to make "b" Uppercase and rest lowercase also how to make first letter Uppercase and rest lowercase?
Like "john" >> Output >> "John"...
I have toUppercase() and toLowercase(). They convert the whole text.
Also I tried to include charAt but never worked with me.
You will need to take your string, take a substring of the specific character or characters you want to capitalize or lowercase, and then build a new string off of it.
Example
String test = "JoHn"; //make the H lowercase
test = test.substring(0,2) + test.substring(2,3).toLowercase() + test.substring(3);
The first substring gets all characters before the desired point, the second gets the desired character and lowercases it, and the final substring gets the rest of the string
You can use toCharArray() to capitalize the first letter like this:
String name = "robert";
// Convert String to char array.
char[] arr = name.toCharArray();
// Modify first element in array.
arr[0] = Character.toUpperCase(arr[0]);
String str = new String(arr);
System.out.println(str);
Output:
Robert
And you want to make "b" Uppercase and rest lowercase like this:
// Convert String to char array.
char[] arr2 = name.toCharArray();
// Modify the third element in array.
arr2[2] = Character.toUpperCase(arr2[2]);
String str2 = new String(arr2);
System.out.println(str2);
Output:
roBert
//Try this...
String str = "Robert";
for (int i = 0; i < str.length(); i++) {
int aChar = str.charAt(i);
// you can directly use character instead of ascii codes
if (aChar == 'b') {
aChar = aChar - 32;
} else if (aChar >= 'A' && aChar <= 'Z') {
aChar += 32 ;
}
System.out.print((char) aChar);
}
/*
Output will be- roBert
*/
I wouldn't use 'test.substring(2, 3).toLowerCase()' necessarily. 'Character.valueOf(test.charAt(2)).toUpperCase()' works. Also, the 'test.substring(0, 3)' is wrong; it should be 'test.substring(0, 2)'.
A function that capitalize the first letter
private String capitalize(String str) {
return Character.toUpperCase(str.charAt(0)) + str.substring(1);
}
A function that capitalize an arbitrary letter
private String replaceCharWithUpperCase(char letterToCapitalize, String str)
{
return str.replaceAll(letterToCapitalize, Character.toUpperCase(letterToCapitalize));
}
Then you can use the previous functions like that :
String a = "JOHN";
a = capitalize(a.toLowerCase());
// now a = John.
String b = "ROBERT";
a = replaceCharWithUpperCase('b', a.toLowerCase());
// now a = roBert.
I have String variable with value- f.e.:
this is test-str-ing_łóśżćń.
And I would like replace this chars:
, -, ł,ó,ś,ż,ć,ń
with those:
_,_,l,o,s,z,c,n.
And I mean here, that if parser will found f.e.: char - (which is second in first list) should be replaced with char that is in the same position/place in second list, which in this example is: _.
The char ó should be replaced with char o.
The char ń should be replaced with char n.
In my case the list of characters to replace is quite long and parsing in loop for each char to replace would not be enought efficient.
I know method replaceAll(). but it only accept one in String and one out String
So I am looking for method, that will allow me to work on arrays/list of Strings instead of single String.
Please give me some help.
Use java.text.Normalizer to Decompose accented letters in base letter plus "combining diacritical marks."
String base = Normalizer.normalize(accented, Form.NFKD)
.replaceAll("\\p{M}", "");
This does a decompose (D) normalization, and then removes Marks.
Some replacements still needed.
char[] out = new char[src.length()];
for( j ...){
inputChar = src.charAt(j);
for (int i = 0; i < convertChars.length; i++) {
if (inputChar == convertChars[i]) {
inputChar = toChars[i];
}
}
}
out[j] = inputChar ;
}
out2 = new String(out);
Extracted from bigger code without IDE, not tested. Loop (I hope) don't allocate objects and should not degrade speed.
Make a static lookup table:
private static char[] substitutions = new char[65536];
static {
// Initialize
for (char c = 0; c < substitutions.length; c++) {
substitutions[c] = c;
}
// Now add mappings.
substitions['-'] = '_'; // Map source->target character
... // Add the rest
}
// LATER IN Code
char[] stringChars = inputString.toCharArray();
for (int i = 0; i < stringChars.length; i++) {
stringChars[i] = substitutions[stringChars[i]];
}
outputString = new String(stringChars);
I have a String entered by the User.
I'm trying to replace all non-uppercase letters(i.e. numbers, or symbols) with spaces without using methods such as replace(), Stringbuilders, or arrays.
This is what I have so far :
public static void formatPlaintext(String sentence){
String sentenceUpper = sentence.toUpperCase();
System.out.println(sentenceUpper);
String emptyString = " ";
for(int i = 0; i< sentenceUpper.length() ; i++){
char ch = sentenceUpper.charAt(i);
if(ch < 'A' || ch > 'Z'){
ch = emptyString.charAt(0);
}
}
}//end of formatPlaintext
I keep getting the error that String index is out of range. I believe it has to do with :
ch = emptyString.charAt(0);
because emptyString doesn't have any characters. But even if I put an arbitrary constant in, it doesn't replace the non-letters with this arbitrary constant.
This isn't how you replace characters in a Java string. In Java, strings are immutable, so you can't set any given index. Additionally, the charAt() method doesn't and can't do anything to the string you're calling it on - all it does is just return the char at that position. Lastly, you probably shouldn't be using void - return the String with characters replaced at the end of the method, then use the return value. You can accomplish this by iterating through your initial string and build a new string, using the static isUpperCase method of the Character class:
public static String formatPlainText(String sentence)
{
String replacedSentence = "";
for(int i = 0; i< sentence.length() ; i++){
char ch = sentence.charAt(i);
if (Character.isUpperCase(ch)) {
replacedSentence += ch;
}
else {
replacedSentence += " ";
}
}
return replacedSentence;
}
If you're going to be using this frequently, or for particularly long Strings, you should absolutely use a StringBuilder instead - concatenating String on a character-by-character basis is deeply inefficient.
You have to remember that arguments in Java are passed as values, not as references, and in this case the String is an immutable object, i.e. an String cannot be changed, when you do a concatenation or replace you're effectively creating a new String.
What I would recommend is to modify your code a little bit to return the new String that was built.
public static String formatPlaintext(String sentence){
String sentenceUpper = sentence.toUpperCase();
System.out.println(sentenceUpper);
StringBuilder builder = new StringBuilder;
for(int i = 0; i< sentenceUpper.length() ; i++){
char ch = sentenceUpper.charAt(i);
if(ch < 'A' || ch > 'Z'){
builder.append(' ');
} else {
builder.append(ch);
}
}
return builder.toString();
}