How do I move char characters to left or to right in a string?
Reading the input string backwards you need to keep every character on an odd index of each word and any blank characters.
You could start with this snippet. See it as a PoC to demonstrate the logic. Optimisations are possible.
String encoded = "bxoqb swi eymrawn yim";
StringBuilder decoded = new StringBuilder();
boolean keep = true;
for (int i = encoded.length() - 1; i >= 0; i--) {
if (encoded.charAt(i) != ' ') {
if (keep) {
decoded.append(encoded.charAt(i));
}
keep = !keep;
} else {
decoded.append(' ');
keep = true;
}
}
System.out.println("decoded = " + decoded);
output
decoded = my name is bob
explanation
the for-loop processes the string backwards, so the characters are processed as miy nwarmye iws bqoxb
the variable i hold the current index position in the string encoded
as we want to keep only the characters on odd positions in a word the variable keep is used as a indicator
when the variable keep is true we append the current character (the one on position i in string encoded) to the string buffer decoded
if the current processed character is not a the value of keepis negated (true->false, false->true), so we append characters on every odd position
as we need to keep between the words also we have to treat this separately, each is appended to decoded and keep is set to true so the next non-blank character would be added too
Try this:
StringBuilder builder = new StringBuilder();
String[] charArray = encoded.split(" ");
for(int i = charArray.length-1 ; i >= 0; i--){
builder.append(charArray[i]);
}
String decoded = builder.toString();
You have to use StringBuffer to reverse the sentence.Then you can split your sentence word by word using the spaces between the words. After that basic java knowledge ...
String ss = "bxoqb swi eymrawn yim";
StringBuilder buffer = new StringBuilder(ss);
String word[] = buffer.reverse().toString().split(" ");
for (String word1 : word) {
char c[]=word1.toCharArray();
for(int x=0;x<c.length;x++){
if(x%2==0){
System.out.print(c[x]);
}
}
System.out.print(" ");
}
Related
There is a way to split a string into repeating characters using a regex function but I want to do it without using it.
for example, given a string like: "EE B" my output will be an array of strings e.g
{"EE", " ", "B"}
my approach is:
given a string I will first find the number of unique characters in a string so I know the size of the array. Then I will change the string to an array of characters. Then I will check if the next character is the same or not. if it is the same then append them together if not begin a new string.
my code so far..
String myinput = "EE B";
char[] cinput = new char[myinput.length()];
cinput = myinput.toCharArray(); //turn string to array of characters
int uniquecha = myinput.length();
for (int i = 0; i < cinput.length; i++) {
if (i != myinput.indexOf(cinput[i])) {
uniquecha--;
} //this should give me the number of unique characters
String[] returninput = new String[uniquecha];
Arrays.fill(returninput, "");
for (int i = 0; i < uniquecha; i++) {
returninput[i] = "" + myinput.charAt(i);
for (int j = 0; j < myinput.length - 1; j++) {
if (myinput.charAt(j) == myinput.charAt(j + 1)) {
returninput[j] += myinput.charAt(j + 1);
} else {
break;
}
}
} return returninput;
but there is something wrong with the second part as I cant figure out why it is not beginning a new string when the character changes.
You question says that you don't want to use regex, but I see no reason for that requirement, other than this is maybe homework. If you are open to using regex here, then there is a one line solution which splits your input string on the following pattern:
(?<=\S)(?=\s)|(?<=\s)(?=\S)
This pattern uses lookarounds to split whenever what precedes is a non whitespace character and what proceeds is a whitespace character, or vice-versa.
String input = "EE B";
String[] parts = input.split("(?<=\\S)(?=\\s)|(?<=\\s)(?=\\S)");
System.out.println(Arrays.toString(parts));
[EE, , B]
^^ a single space character in the middle
Demo
If I understood correctly, you want to split the characters in a string so that similar-consecutive characters stay together. If that's the case, here is how I would do it:
public static ArrayList<String> splitString(String str)
{
ArrayList<String> output = new ArrayList<>();
String combo = "";
//iterates through all the characters in the input
for(char c: str.toCharArray()) {
//check if the current char is equal to the last added char
if(combo.length() > 0 && c != combo.charAt(combo.length() - 1)) {
output.add(combo);
combo = "";
}
combo += c;
}
output.add(combo); //adds the last character
return output;
}
Note that instead of using an array (has a fixed size) to store the output, I used an ArrayList, which has a variable size. Also, instead of checking the next character for equality with the current one, I preferred to use the last character for that. The variable combo is used to temporarily store the characters before they go to output.
Now, here is one way to print the result following your guidelines:
public static void main(String[] args)
{
String input = "EEEE BCD DdA";
ArrayList<String> output = splitString(input);
System.out.print("[");
for(int i = 0; i < output.size(); i++) {
System.out.print("\"" + output.get(i) + "\"");
if(i != output.size()-1)
System.out.print(", ");
}
System.out.println("]");
}
The output when running the above code will be:
["EEEE", " ", "B", "C", "D", " ", "D", "d", "A"]
I have a string as follows:
String sentence = "I have bananas\r" +
"He has apples\r" +
"I own 3 cars\n" +
"*!"
I'd like to reverse this string so as to have an output like this:
"*!" +
"\ncars 3 own I" +
"\rapples has He" +
"\rbananas have I"
Here is a program I wrote.
public static String reverseWords(String sentence) {
StringBuilder str = new StringBuilder();
String[] arr = sentence.split(" ");
for (int i = arr.length -1; i>=0; i--){
str.append(arr[i]).append(" ");
}
return str.toString();
}
But I don't get the output as expected. What is wrong?
The problem is you are only splitting on spaces, but that is not the only type of whitespace in your sentence. You can use the pattern \s to match all whitespace. However, then you don't know what to put back in that position after the split. So instead we will split on the zero-width position in front of or behind a whitespace character.
Change your split to this:
String[] arr = sentence.split("(?<=\\s)|(?=\\s)");
Also, now that you are preserving the whitespace characters, you no longer need to append them. So change your append to this:
str.append(arr[i]);
The final problem is that your output will be garbled due to the presence of \r. So, if you want to see the result clearly, you should replace those characters. For example:
System.out.println(reverseWords(sentence).replaceAll("\\r","\\\\r").replaceAll("\\n","\\\\n"));
This modified code now give the desired output.
Output:
*!\ncars 3 own I\rapples has He\rbananas have I
Note:
Since you are freely mixing \r and \n, I did not add any code to treat \r\n as a special case, which means that it will be reversed to become \n\r. If that is a problem, then you will need to prevent or undo that reversal.
For example, this slightly more complex regex will prevent us from reversing any consecutive whitespace characters:
String[] arr = sentence.split("(?<=\\s)(?!\\s)|(?<!\\s)(?=\\s)");
The above regex will match the zero-width position where there is whitespace behind but not ahead OR where there is whitespace ahead but not behind. So it won't split in the middle of consecutive whitespaces, and the order of sequences such as \r\n will be preserved.
The logic behind this question is simple, there are two steps to achieve the OP's target:
reverse the whole string;
reverse the words between (words splitted by spaces);
Instead of using StringBuilder, I'd prefer char[] to finish this, which is easy to understand.
The local test code is:
public class WordReverse {
public static void main(String... args) {
String s = " We have bananas\r" +
"He has apples\r" +
"I own 3 cars\n" +
"*!";
System.out.println(reverseSentenceThenWord(s));
}
/**
* return itself if the #param s is null or empty;
* #param s
* #return the words (non-whitespace character compound) reversed string;
*/
private static String reverseSentenceThenWord(String s) {
if (s == null || s.length() == 0) return s;
char[] arr = s.toCharArray();
int len = arr.length;
reverse(arr, 0, len - 1);
boolean inWord = !isSpace(arr[0]); // used to track the start and end of a word;
int start = inWord ? 0 : -1; // is the start valid?
for (int i = 0; i < len; ++i) {
if (!isSpace(arr[i])) {
if (!inWord) {
inWord = true;
start = i; // just set the start index of the new word;
}
} else {
if (inWord) { // from word to space, we do the reverse for the traversed word;
reverse(arr, start, i - 1);
}
inWord = false;
}
}
if (inWord) reverse(arr, start, len - 1); // reverse the last word if it ends the sentence;
String ret = new String(arr);
ret = showWhiteSpaces(ret);
// uncomment the line above to present all whitespace escape characters;
return ret;
}
private static void reverse(char[] arr, int i, int j) {
while (i < j) {
char c = arr[i];
arr[i] = arr[j];
arr[j] = c;
i++;
j--;
}
}
private static boolean isSpace(char c) {
return String.valueOf(c).matches("\\s");
}
private static String showWhiteSpaces(String s) {
String[] hidden = {"\t", "\n", "\f", "\r"};
String[] show = {"\\\\t", "\\\\n", "\\\\f", "\\\\r"};
for (int i = hidden.length - 1; i >= 0; i--) {
s = s.replaceAll(hidden[i], show[i]);
}
return s;
}
}
The output is not in my PC as OP provided but as:
*!
bananas have I
However, if you set a breakpoint and debug it and check the returned string, it will be as:
which is the right answer.
UPDATE
Now, if you would like to show the escaped whitespaces, you can just uncomment this line before returning the result:
// ret = showWhiteSpaces(ret);
And the final output will be exactly the same as expected in the OP's question:
*!\ncars 3 own I\rapples has He\rbananas have I
Take a look at the output you're after carefully. You actually need two iteration steps here - you first need to iterate over all the lines backwards, then all the words in each line backwards. At present you're just splitting once by space (not by new line) and iterating over everything returned in that backwards, which won't do what you want!
Take a look at the example below - I've kept closely to your style and just added a second loop. It first iterates over new lines (either by \n or by \r, since split() takes a regex), then by words in each of those lines.
Note however this comes with a caveat - it won't preserve the \r and the \n. For that you'd need to use lookahead / lookbehind in your split to preserve the delimiters (see here for an example.)
public static String reverseWords(String sentence) {
StringBuilder str = new StringBuilder();
String[] lines = sentence.split("[\n\r]");
for (int i = lines.length - 1; i >= 0; i--) {
String[] words = lines[i].split(" ");
for (int j = words.length - 1; j >= 0; j--) {
str.append(words[j]).append(" ");
}
str.append("\n");
}
return str.toString();
}
My problem is that I'm getting a String and I need to check if there is a space in the 4th position but starting from the end. If in this position there is not a space, I should insert it.
For example:
I get this String: TW12EF, need to get it like this: TW1 2EF
First of all I get the 4 last characters in a char array because I also need to check if they are numbers or letters.
With this method I check if there is a space:
public static boolean isSpace(){
return String.valueOf(charArray[0]).matches("[ \\t\\n\\x0B\\f\\r]");
}
charArray contains the last 4 characters of the input String
If charArray[0] wouldn't be a space, I want to insert a space in the 2nd place (charArray[1])
If there is something that I can correct in the question to make it easier to understand, just let me know and I will try to make it better for next questions.
A simple and direct solution (most likely faster than using a regular expression) is to get the 4th to the last character (if it exists), and if it isn't a white-space, insert a space at that position.
public static void main(String[] args) {
String str = "TW12EF";
int insertPos = str.length() - 4;
if (insertPos >= 0) {
char ch = str.charAt(insertPos);
if (!Character.isWhitespace(ch)) {
str = new StringBuilder(str).insert(insertPos + 1, ' ').toString();
}
}
System.out.println(str);
}
A whitespace is determined by invoking isWhitespace, which returns true for space but also tabs or line feeds, like you did in your question. The character is inserted by leveraging the StringBuilder#insert method, which is more direct that taking 2 substrings and concatenating them.
A quick, dirty regex will help :
String p = "TW12EF";
System.out.println(p.replaceAll("(.)\\s*(\\S.{2})$", "$1 $2")); // Select a character followed by 0 or more spaces and followed by 3 non-space characters. And replace multiple spaces if they exist with a single space
O/P :
TW1 2EF
Also works if there are one or more spaces after the 3rd char (from the left)
As char is a primitive data type, the comparison can be done simply with
if (charArray[0] == ' ') {
char[] temp = new char[5];
temp[0] = ' ';
for (int i = 1; i <= 4; i++) {
temp[i] = charArray[i - 1];
}
charArray = temp;
}
You could use something like:
public static void main(String[] args) {
String str = "TW12EF";
processStr(str);
}
public static final int SPACE_POS = 4, OFFSET = 1;
public static String processStr(String str)
{
if(!Character.isWhitespace(str.charAt(str.length() - SPACE_POS)))
{
str = String.format("%s %s", str.substring(0, str.length() - SPACE_POS + OFFSET), str.substring(SPACE_POS - OFFSET));
}
return str;
}
Like this?
` String s="TW12EF";
String result="";
int length=s.length();
for(int i=length-1;i>-1;i--){
if(i==length-4&&s.charAt(i)!=' '){
result+=" ";
}
result+=s.charAt(length-i-1);
}
System.out.println(result);`
Hi I'm trying to insert a dash in between all of the characters in a string. I've done this, but it wont work:
public static String expand (String word)
{
int stringLength = word.length();
for (int x=0; x<stringLength; x++){
word = new StringBuffer(word).insert(x, "-").toString();
}
return word;
}
It results in the dashes before the word. I don't understand why it's not working. Thanks.
That's right, all dashes get inserted before the word. Here is how this happens: when you insert the first dash, what used to be at index one is moved by one character, so when you insert the dash at the next position, you insert it right after the previous dash: the word keeps moving away, so you loop behaves like a dog chasing its own tail!
There are several ways to fix this problem:
Insert dashes at even locations, i.e. 2*i
Start with an empty StringBuffer, go through the original characters in a loop, and add a character followed by a dash; when you are at the last character, do not add a dash.
Note that the second approach is more efficient, because it is linear in the number of characters in the original word. The first approach (i.e. one based on insertions) is less efficient, because it is O(n2) due to the need to shift the tail of the buffer on each insertion.
The issue is that you are adding a new character into the string, but not incrementing the index to account for that.
Try this (note, I haven't tested it, but it should work correctly. If you have an extra dash at start or finish, just remove them afterward):
public static String expand (String word)
{
int stringLength = word.length();
for (int x=1; x<(stringLength-1)*2; x+=2){
word = new StringBuffer(word).insert(x, "-").toString();
}
return word;
}
Form your output NOT using the input in this function, and make sure not to insert a hyphen after the last character:
public static String expand(String word) {
int stringLength = word.length();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < stringLength - 1; i++) {
buffer.append(word.substring(i, i + 1));
buffer.append("-");
}
buffer.append(word.substring(stringLength - 1, stringLength));
return buffer.toString();
}
Try this:
String word = "hello";
char[] wordChar = word.toCharArray(); //split String into char array
StringBuffer result = new StringBuffer("");
for (char c : wordChar) {
result.append(c).append("-"); // iterate over the letters and append a dash
}
result.deleteCharAt(result.length() - 1); // remove last dash
System.out.println(result.toString());
Output: "h-e-l-l-o"
"Split" & Conquer ;)
I am working on this assignment and I wanted to know how can I display sequence in groups of 10 characters at a time.
Below is the working program screenshot:
I want to group 10 characters in the output box, for example:
1 CTCTAACGCG CAAGCGCATA TCCTTCTAGG
61 ....
There are about 60 characters in each line excluding spaces and the number, so there must be 6 groups of 10 characters.
Below is the code I made to display this output:
public void dispLines() {
// Get the selected value of characters per line and assign it to noc variable
String noc = numOfChar.getSelectedItem().toString();
// Call StringBuffer object and assign sb variable to it
StringBuffer sb = new StringBuffer();
// Assign raw dna data to dna variable, where string will be mutated
String dna = rawDNAInput.getText();
// Create newdna variable to store the newly created data
String newdna = "";
// Loop through the size of raw dna
for (int i = 0 ; i < dna.length (); ++i)
{
// Assign every single character to StringBuffer sb
sb.append(dna.charAt (i));
}
// Assign the StringBuffer sb values to the newdna variable
newdna = sb.toString();
// Recall StringBuffer object, so new data can be assigned
sb = new StringBuffer();
// Assign start varaible of 0
int start = 0;
// Assign end varaible to be start + number of characters per line
int end = start + Integer.parseInt(noc);
// Keep looping till end value is less than the length of the dna
while(end < newdna.length())
{
// Append values into StringBuffer sb varaible by calling makeNumberedStr method
sb.append(makeNumberedStr(newdna.substring(start, end), start + 1));
// Increment start variable by the selected numbers of characters per line
start += Integer.parseInt(noc);
// Increment end variable by the selected numbers of characters per line
end += Integer.parseInt(noc);
}
// Append values into StringBuffer sb varaible by calling makeNumberedStr method
sb.append (makeNumberedStr (newdna.substring (start), start + 1));
String result = sb.toString();
for(int i = 0; i < result.length(); i++) {
}
// Check to make sure uppercase is selected, if it is then make every character uppercase, else make them lowercase
if(upperCase.isSelected()) {
DNAOutput.setText(result.toUpperCase());
} else if(lowerCase.isSelected()) {
DNAOutput.setText(result.toLowerCase());
}
}
/*
* makeNumberedStr
* This method only displays required number of characters per line
* #parameters String x and integer num
* #returns new StringBuffer value
*/
private String makeNumberedStr (String s, int num)
{
// makes and returns a string composed from left to right of:
// a 6 character field containing right justified [num] followed by 2 spaces
// the string s followed by \n
// Call new StringBuffer object and give it a length of raw dna + 8
StringBuffer sb = new StringBuffer (s.length ());
// Create nstr String varaible and give it value of num
String nstr = String.valueOf (num);
// Loop through the nstr length and append blank space
for (int i = 0 ; i < 6 - nstr.length () ; ++i)
sb.append (' ');
// Check if display number is selected, or else do not display number on every line
if(indexNum.isSelected() == true)
sb.append (nstr + " ");
// Append s value to String Buffer
sb.append (s);
// Append new line to StringBuffer
sb.append ('\n');
// Return StringBuffer text
return sb.toString();
}
Thank You, much appreciated!
Run this program, so you have a long string "s", after that i just add code(which automatically count the character's, when it counting reach's to ten, it will automatically put space between the,,), which will help you to add spaces after every ten character, even you don't need to count them...
public class PracticeOne {
public static void main(String [] args)
{
String s = "aaaaaaaaaaaaaaaaaaaaaaaaa";
System.out.println(s.replaceAll(".{10}", "$0 "));
}
}
the result is
aaaaaaaaaa aaaaaaaaaa aaaaa
hope this will help you
Without using regular expressions (which is how Akshay GOel's answer was produced) we can add spaces to a StringBuffer with a method like the one below. I think you are using 5 or 6 characters at the start of each line for a number.
//Inserts spaces every 10 characters.
//#parm from The index of the buf to begin counting to insert the spaces.
private static void addSpaces(StringBuilder buf, int from) {
for(int i=from+10; i<buf.length(); i+=11) {
buf.insert(i,' ');
// i++;
}
}