I'm trying to convert some text so that every even character becomes uppercase. This works, but if there's a space between words, the code takes the space as a character too. So for example, if the input text is "this is a test", the output is "tHiS Is a tEsT". I want it to ignore the spaces and give "tHiS iS a TeSt" as output.
I now have the following code:
private String result;
private String letter;
private void generateText() {
result = "";
String input = editTextInput.getText().toString();
String lowerCase = input.toLowerCase();
char[] charArray = lowerCase.toCharArray();
for(int i=0;i<charArray.length;i++){
if(String.valueOf(charArray[i]).equals(" ")){
//I don't know what to put here
letter = String.valueOf(charArray[i]);
}else{
if(i%2 == 0){
letter = String.valueOf(charArray[i]);
}else if(i%2 == 1){
letter = String.valueOf(charArray[i]).toUpperCase();
}
}
result += letter ;
}
Log.d("result", result);
}
What do I have to do to skip the spaces?
If it's possible, I would like to skip punctuation marks too, or in general, every character which is not a letter.
Thanks in advance!
(For those who are wondering, I'm making a Spongebob meme text generator app)
If you want to do alternate logic in a loop, you could normally use i % 2 == 0, or (i & 1) == 1, but since the alternation is conditional, you need a variable to store the "state". With simple alternation, a boolean variable is the obvious choice.
Also, continuously converting each char to a String is bad for performance. Just update the char[].
private static String upperEven(String input) {
char[] buf = input.toLowerCase().toCharArray();
boolean upper = false;
for (int i = 0; i < buf.length; i++) {
if (Character.isLetter(buf[i])) {
if (upper)
buf[i] = Character.toUpperCase(buf[i]);
upper = ! upper;
}
}
return new String(buf);
}
Test
System.out.println(upperEven("this IS a TEST"));
Output
tHiS iS a TeSt
Code can be compressed/obscured to this: ;-)
private static String upperEven(String s) {
char[] c = s.toCharArray();
boolean t = false;
for (int i = 0; i < c.length; i++)
if (Character.isLetter(c[i]))
c[i] = ((t = ! t) ? Character.toLowerCase(c[i]) : Character.toUpperCase(c[i]));
return new String(c);
}
This is my solution.
private static void generateText() {
String result = "";
String input = "i am a engineer and student of nit.";
String lowerCase = input.toLowerCase();
Boolean isLower = false;
char[] charArray = lowerCase.toCharArray();
for (int i = 0; i < lowerCase.length(); i++) {
String letter = String.valueOf(charArray[i]);
if (!Character.isLetter(charArray[i])) {
result += letter;
} else {
if(isLower)
letter = letter.toUpperCase();
result += letter;
isLower = !isLower;
}
}
System.out.println(result);
}
Related
public class Main {
public static void main(String[] args) {
String name = "the-stealth-warrior";
for (int i = 0; i < name.length();i++){
if (name.charAt(i) == '-'){
char newName = Character.toUpperCase(name.charAt(i+1));
newName += name.charAt(i + 1);
i++;
}
}
}
}
I try to loop in every char and check if the I == '-' convert the next letter to be uppercase and append to a new String.
We can try using a split approach with the help of a stream:
String name = "the-stealth-warrior";
String parts = name.replaceAll("^.*?-", "");
String output = Arrays.stream(parts.split("-"))
.map(x -> x.substring(0, 1).toUpperCase() + x.substring(1))
.collect(Collectors.joining(""));
output = name.split("-", 2)[0] + output;
System.out.println(output); // theStealthWarrior
I think the most concise way to do this would be with regexes:
String newName = Pattern.compile("-+(.)?").matcher(name).replaceAll(mr -> mr.group(1).toUpperCase());
Note that Pattern.compile(...) can be stored rather than re-evaluating it each time.
A more verbose (but probably more efficient way) to do it would be to build the string using a StringBuilder:
StringBuilder sb = new StringBuilder(name.length());
boolean uc = false; // Flag to know whether to uppercase the char.
int len = name.codePointsCount(0, name.length());
for (int i = 0; i < name.len; ++i) {
int c = name.codePointAt(i);
if (c == '-') {
// Don't append the codepoint, but flag to uppercase the next codepoint
// that isn't a '-'.
uc = true;
} else {
if (uc) {
c = Character.toUpperCase(c);
uc = false;
}
sb.appendCodePoint(c);
}
}
String newName = sb.toString();
Note that you can't reliably uppercase single codepoints in specific locales, e.g. ß in Locale.GERMAN.
I am trying to implement a function that searches a given string and reverses capitalization of all occurrences of particular alphabetical characters (case insensitive), leaving numbers, special characters, and other alphabetical characters unaffected.
For example, if theString = "abc123XYZ" and reverseCaps= "cyz", the result should be "abC123Xyz".
I have tried various implementations and fixes, but cannot get it to work properly. The result I am getting now is "ABC123xyz".
Here is my code:
public static String flipCaseChars(String theString, String reverseCap) {
StringBuilder buf = new StringBuilder(theString.length());
for (int i = 0; i < theString.length(); i++) {
char c = theString.charAt(i);
if (Character.isUpperCase(c)) {
buf.append(Character.toLowerCase(c));
}
else if (Character.isLowerCase(c)) {
buf.append(Character.toUpperCase(c));
}
// if char is neither upper nor lower
else {
buf.append(c);
}
}
return buf.toString();
}
What should I do? Any help would be very much appreciated.
public static String flipCaseChars(String theString, String reverseCap) {
StringBuilder buf = new StringBuilder(theString.length());
for (int i = 0; i < theString.length(); i++) {
char c = theString.charAt(i);
if (reverseCap.indexOf(c) >= 0){
if (Character.isUpperCase(c)) {
buf.append(Character.toLowerCase(c));
} else if (Character.isLowerCase(c)) {
buf.append(Character.toUpperCase(c));
} else {
buf.append(c);
}
} else {
buf.append(c);
}
}
return buf.toString();
}
Am I missing something here? I see that some answers here are getting voted up but they're not doing what the OP has requested. According to the example input ("abc123XYZ") and output ("abC123Xyz"), I see it that the letter-case of the characters within the reverseCaps string variable is irrelevant. They could have been any letter-case but, if any one of them are encountered within the input string (theString) regardless of current letter-case state any one of the supplied characters letter-case if flipped to its' opposing state.
So if the Input String was: ab-c-123-C-XYz and the reverseCaps variable contained "cyz" then the output should be: ab-C-123-c-XyZ. Am I mistaken?
If I'm not mistaken then the following code will carry out the task explained above:
public static String flipCaseCharacters(String inputString, String charactersToFlip) {
StringBuilder newString = new StringBuilder();
for (char inChar : inputString.toCharArray()) {
for (char ctFlip : charactersToFlip.toCharArray()) {
if (Character.toUpperCase(inChar) == Character.toUpperCase(ctFlip)) {
inChar = Character.isUpperCase(inChar) ?
Character.toLowerCase(inChar) :
Character.toUpperCase(inChar);
break;
}
}
newString.append(inChar);
}
return newString.toString();
}
An easier way is to loop through the reverseCap String and do a conditional replace
for (char c : reverseCap.toCharArray()) {
if (Character.isLowerCase(c)) {
theString = theString.replace(c, Character.toUpperCase(c));
}
else {
theString = theString.replace(c, Character.toLowerCase(c));
}
}
return theString;
No need to check if the character is upper or lower case. It just flips case of the character as appropriate. This presumes that the reverse list of characters is all lowercase, as shown in the example.
It works by checking and then manipulating the bit 0x20that determines upper and lower case in ASCII characters.
The ^ is the exclusive OR operator that flips to the opposite case by flipping the case bit.
public static String flipCaseChars(String theString, String reverseCap) {
StringBuilder sb = new StringBuilder();
for (char c : theString.toCharArray()) {
// is the character in the list?
if (reverseCap.indexOf(c | 0x20) >= 0) {
c ^= 0x20; // flip the case
}
sb.append(c);
}
return sb.toString();
}
I basically made a HashSet<Character> of reverseCap and then the rest follows #toootooo's answer
Here it is:
static String flipCaseChars(String theString, String reverseCap) {
final StringBuilder stringBuilder = new StringBuilder(theString.length());
HashSet<Character> collect = new HashSet<>();
for (int i = 0; i < reverseCap.length(); i++) {
collect.add(Character.toLowerCase(reverseCap.charAt(i)));
collect.add(Character.toUpperCase(reverseCap.charAt(i)));
}
for (int i = 0; i < theString.length(); i++) {
char currentChar = theString.charAt(i);
if (collect.contains(currentChar)) {
if (Character.isUpperCase(currentChar)) {
currentChar = Character.toLowerCase(currentChar);
} else if (Character.isLowerCase(currentChar)){
currentChar = Character.toUpperCase(currentChar);
}
}
stringBuilder.append(currentChar);
}
return stringBuilder.toString();
}
The only advantage of this approach is that the lookup of the characters in reverseCap is done in constant time and the time complexity is directly propertional to the length of theString.
The goal of this program is to prompt the user for a single character and a phrase, and then replace any instances of that character within that phrase with a '$'. My program below does just that, but when I showed it to my professor I was told that I cannot use .replace in the methods I built, so I have to figure out a way to not use that. I have worked at it for a while, and thus far I know that I can replace it with a for loop, but after several frustrating iterations, I can't seem to get it right. Excuse me if my code looks funky, I am still an introductory java student so I'm still learning the basics. I have provided a proposed solution at the end of my code snippet below.
public static char getKeyCharacter(String userInput) {
char keyCharacter;
Scanner inputStream = new Scanner(System.in);
while(userInput.length() > 1)
{
System.out.println("Please enter a SINGLE character to use as key: ");
userInput = inputStream.nextLine();
}
keyCharacter = userInput.charAt(0);
return keyCharacter;
}
public static String getString(String userResponse) {
Scanner inputStream = new Scanner(System.in);
String theString;
while(userResponse.length() > 500) {
System.out.println("Please enter a phrase or sentence >= 4 and <=500 characters: ");
userResponse = inputStream.nextLine();
}
while(userResponse.length() < 4) {
System.out.println("Please enter a phrase or sentence >= 4 and <=500 characters: ");
userResponse = inputStream.nextLine();
}
theString = userResponse;
return theString;
}
public static String maskCharacter(String theString, char keyCharacter){
String maskedString = "";
final char mask = '$';
maskedString = maskedString + theString.replace(keyCharacter, mask);
System.out.println("String with " + keyCharacter + " masked: ");
return maskedString;
}
public static String removeCharacter(String theString, char keyCharacter) {
String modifiedString = " ";
final char replaceChar = ' ';
modifiedString = modifiedString + theString.replace(keyCharacter, replaceChar);
System.out.println("String with " + keyCharacter + " removed:");
return modifiedString;
}
public static int countKey(String theString, char keyCharacter) {
int charCount = 0;
for (int c = 0; c < theString.length(); c++) {
if (theString.charAt(c) == keyCharacter) {
charCount++;
}
}
System.out.println("Occurences of " + keyCharacter + " in string:");
return charCount;
}
}
I believe the solution is will look something like this, but thus far I've been unsuccesful -
public static String maskCharacter(String theString, char keyCharacter){
String maskedString = "";
final char mask = '$';
for (int k = 0; k < theString.length(); k++) {
if (theString.charAt(k) == keyCharacter) {
keyCharacter = mask;
}
System.out.println("String with " + keyCharacter + " masked: ");
return maskedString;
}
My issue lies in making the maskedString = theString with all the keyCharacters replaced by mask. For the record, I have yet to learn anything about those fancy arrays, so if there is a way to do this using a simple for loop I would greatly appreciate it. Thank you for the assistance in advance!
I would use a StringBuilder and String#toCharArray() with a simple for-each loop. Like,
public static String maskCharacter(String theString, char keyCharacter){
StringBuilder sb = new StringBuilder();
for (char ch : theString.toCharArray()) {
if (ch == keyCharacter) {
sb.append('$'); // <-- mask keyCharacter(s).
} else {
sb.append(ch); // <-- it isn't the character to mask
}
}
return sb.toString();
}
I wouldn't use a StringBuilder: just use the result of toCharArray() directly:
char[] cs = theString.toCharArray();
for (int i = 0; i < cs.length; ++i) {
if (cs[i] == keyCharacter) cs[i] = '$';
}
return new String(cs);
Not only is it more concise, but:
It will run faster, because it's cheaper to access an array element than to invoke a method; and because it doesn't require StringBuilder's internal buffer to resize (although you could just pre-size that);
It will use less memory, because it doesn't require storage for the copy inside StringBuilder.
public static String maskCharacter(String theString, char keyCharacter){
String masked = "";
for (int i = 0 ; i < theString.length() ; i++) {
if (theString.charAt(i) == keyCharacter) {
masked += "$";
}
else {
masked+=theString.charAt(i)+"";
}
}
return masked;
}
An answer that only uses string concatenation and basic character access.
You seem to know that you can concatenate something to a string and get a different string.
maskedString = maskedString + ...;
You also know you can build a for-loop that gets each individual character using .charAt()
for (int k = 0; k < theString.length(); k++) {
char nch = theString.charAt(k);
}
You can check equality between chars
if (nch == keyCharacter)
... assuming you know about else-branches, isn't it clear you just need to put them together?
if (nch == keyCharacter) {
// append '$' to maskedString
}
else {
// append nch to maskedString
}
Of course this creates a new string on every loop iteration so it is not terribly efficient. But I don't think that's the point of the exercise.
I have an assignment for school due at midnight today. I have finished almost all the assignment except for one question. I need to swap "r" and "q" with each other as values. So, if you enter "r" in the compiler you should get "q" if you enter "q" you get "r"(Using JOptionPane). For example, if your name is Quart, the compiler should print Ruaqt. I tried using the replace.All method, but once I can only swap "r" or "q" not both. I know I need a temporary variable, but do not know anything else...
We had to replace vowels with the letter after them so I did this:
String firstName = JOptionPane
.showInputDialog("What is your first name?");
String lastName = JOptionPane
.showInputDialog("What is your last name?");
String fullname = firstname + lastname;
String lowername = fullName.toLowerCase();
String encryptedname = lowername.replaceAll("a", "b")
.replaceAll("e", "f").replaceAll("i", "j").replaceAll("o", "p")
.replaceAll("u", "v");
Thanks
Dunno why the 2 answers using StringBuilder are both making the thing more complicated than needed.
Here is the way you can use StringBuilder to do that single character swap:
public static String swapChar(String string, char c1, char c2) {
StringBuilder sb = new StringBuilder(string);
for (int i = 0; i < sb.length(); ++i) {
if (sb.charAt(i) == c1) {
sb.setCharAt(i, c2);
} else if (sb.charAt(i) == c2) {
sb.setCharAt(i, c1);
}
}
return sb.toString();
}
Update :
Just found that what you are looking for is actually doing a bunch of replace of character at the same time. That can be cleanly done by providing a Map as parameter:
public static String replaceChars(String string, Map<Character,Character> cmap) {
StringBuilder sb = new StringBuilder(string);
for (int i = 0; i < sb.length(); ++i) {
if (cmap.containsKey(sb.charAt(i)) {
sb.setCharAt(i, cmap.get(sb.charAt(i));
}
}
return sb.toString();
}
to use it:
// or make a util method to make these even easier to create
Map<Character,Character> cmap = new HashMap<Character,Character>();
cmap.put('r','q');
cmap.put('q','r');
cmap.put('a','b');
cmap.put('e','f');
cmap.put('i','j');
cmap.put('o','p');
cmap.put('u','v');
and simply do a replace:
String result = replaceChars(inputString, cmap);
or even simpler, by making use of Apache Commons Lang:
String result = StringUtils.replaceChars(inputString, "rqaeiou", "qrbfjpv");
You can try this.
private static final char Q_STR = 'q';
private static final char R_STR = 'r';
public static String replaceString(String original, int position, char strToReplace) {
StringBuilder strBuilder = new StringBuilder(original);
if (strToReplace == Q_STR) {
strBuilder.setCharAt(position, R_STR);
} else if (strToReplace == R_STR){
strBuilder.setCharAt(position, Q_STR);
}
return strBuilder.toString();
}
public static void main(String[] args) {
String firstname = "Quart";
String lastname = " QuartLastName";
String fullname = firstname + lastname;
String lowername = fullname.toLowerCase();
//get all chars in String
char[] array = lowername.toCharArray();
//list to keep original position of Q char
List<Integer> allQPosition = new ArrayList<Integer>();
//list to keep original position of R char
List<Integer> allRPosition = new ArrayList<Integer>();
for (int i = 0; i < array.length; i++) {
if(array[i] == 'q') {
allQPosition.add(i);
} else if(array[i] == 'r') {
allRPosition.add(i);
}
}
//replace q
for (Integer integer : allQPosition) {
lowername = replaceString(lowername, integer, Q_STR);
}
//replace r
for (Integer integer : allRPosition) {
lowername = replaceString(lowername, integer, R_STR);
}
//replace others
String encryptedname = lowername.replace("a", "b")
.replace("e", "f")
.replace("i", "j")
.replace("o", "p")
.replace("u", "v");
System.out.println("Result: " + encryptedname);
}
My solution is:
Keep all position of 'q' and 'r' from original String.
Replace each of them
Replace the rest of other chars
Hope this help
public static void main(String[] args) {
String origin = "r and q";
System.out.println(newReplacement(origin, 'r', 'q'));
}
private static String newReplacement(String origin, char firstChar, char secondChar) {
StringBuffer stringBuffer = new StringBuffer(origin);
for(int i = 0; i < origin.length(); i++) {
if(origin.charAt(i) == firstChar) {
stringBuffer.replace(i, i+1, secondChar + "");
continue;
}
if(origin.charAt(i) == secondChar) {
stringBuffer.replace(i, i+1, firstChar + "");
}
}
return stringBuffer.toString();
}
Rewrite replace method with simple one.
I need to get a new string based on an old one and a lag. Basically, I have a string with the alphabet (s = "abc...xyz") and based on a lag (i.e. 3), the new string should replace the characters in a string I type with the character placed some positions forward (lag). If, let's say, I type "cde" as my string, the output should be "fgh". If any other character is added in the string (apart from space - " "), it should be removed. Here is what I tried, but it doesn't work :
String code = "abcdefghijklmnopqrstuvwxyzabcd"; //my lag is 4 and I added the first 4 characters to
char old; //avoid OutOfRange issues
char nou;
for (int i = 0; i < code.length() - lag; ++i)
{
old = code.charAt(i);
//System.out.print(old + " ");
nou = code.charAt(i + lag);
//System.out.println(nou + " ");
// if (s.indexOf(old) != 0)
// {
s = s.replace(old, nou);
// }
}
I commented the outputs for old and nou (new, but is reserved word) because I have used them only to test if the code from position i to i + lag is working (and it is), but if I uncomment the if statement, it doesn't do anything and I leave it like this, it keeps executing the instructions inside the for statmement for code.length() times, but my string doesn't need to be so long. I have also tried to make the for statement like below, but I got lost.
for (int i = 0; i < s.length(); ++i)
{
....
}
Could you help me with this? Or maybe some advices about how I should think the algorithm?
Thanks!
It doesn't work because, as the javadoc of replace() says:
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
(emphasis mine)
So, the first time you meet an 'a' in the string, you replace all the 'a's by 'd'. But then you go to the next char, and if it's a 'd' that was an 'a' before, you replace it once again, etc. etc.
You shouldn't use replace() at all. Instead, you should simply build a new string, using a StringBuilder, by appending each shifted character of the original string:
String dictionary = "abcdefghijklmnopqrstuvwxyz";
StringBuilder sb = new StringBuilder(input.length());
for (int i = 0; i < input.length(); i++) {
char oldChar = input.charAt(i);
int oldCharPositionInDictionary = dictionary.indexOf(oldChar);
if (oldCharPositionInDictionary >= 0) {
int newCharPositionInDictionary =
(oldCharPositionInDictionary + lag) % dictionary.length();
sb.append(dictionary.charAt(newCharPositionInDictionary));
}
else if (oldChar == ' ') {
sb.append(' ');
}
}
String result = sb.toString();
Try this:
Convert the string to char array.
iterate over each char array and change the char by adding lag
create new String just once (instead of loop) with new String passing char array.
String code = "abcdefghijklmnopqrstuvwxyzabcd";
String s = "abcdef";
char[] ch = s.toCharArray();
char[] codes = code.toCharArray();
for (int i = 0; i < ch.length; ++i)
{
ch[i] = codes[ch[i] - 'a' + 3];
}
String str = new String(ch);
System.out.println(str);
}
My answer is something like this.
It returns one more index to every character.
It reverses every String.
Have a good day!
package org.owls.sof;
import java.util.Scanner;
public class Main {
private static final String CODE = "abcdefghijklmnopqrstuvwxyz"; //my lag is 4 and I added the first 4 characters to
#SuppressWarnings("resource")
public static void main(String[] args) {
System.out.print("insert alphabet >> ");
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
char[] char_arr = s.toCharArray();
for(int i = 0; i < char_arr.length; i++){
int order = CODE.indexOf(char_arr[i]) + 1;
if(order%CODE.length() == 0){
char_arr[i] = CODE.charAt(0);
}else{
char_arr[i] = CODE.charAt(order);
}
}
System.out.println(new String(char_arr));
//reverse
System.out.println(reverse(new String(char_arr)));
}
private static String reverse (String str) {
char[] char_arr = str.toCharArray();
for(int i = 0; i < char_arr.length/2; i++){
char tmp = char_arr[i];
char_arr[i] = char_arr[char_arr.length - i - 1];
char_arr[char_arr.length - i - 1] = tmp;
}
return new String(char_arr);
}
}
String alpha = "abcdefghijklmnopqrstuvwxyzabcd"; // alphabet
int N = alpha.length();
int lag = 3; // shift value
String s = "cde"; // input
StringBuilder sb = new StringBuilder();
for (int i = 0, index; i < s.length(); i++) {
index = s.charAt(i) - 'a';
sb.append(alpha.charAt((index + lag) % N));
}
String op = sb.toString(); // output