Results 1 to 20 of 21
Thread: how can i do this so better?!
- 08-24-2011, 01:10 AM #1
how can i do this so better?!
this is question :
Return a version of the given string, where for every star (*) in the string the star and the chars immediately to its left and right are gone. So "ab*cd" yields "ad" and "ab**cd" also yields "ad".
starOut("ab*cd") → "ad"
starOut("ab**cd") → "ad"
starOut("sm*eilly") → "silly"
===========
and this is my answer
(it's correct but it's very long and i think very idiot)!Java Code:public String starOut(String str) { StringBuilder string = new StringBuilder(str); String res = ""; for(int i=0;i<string.length();i++){ if(string.length()==1 && string.charAt(0)=='*') return ""; if(i==0 && string.charAt(i)=='*'){ string.deleteCharAt(i); string.deleteCharAt(i); i = 0; }else if(i==string.length()-1 && string.charAt(i)=='*'){ string.deleteCharAt(i-1); string.deleteCharAt(i-1); }else if(i<=string.length() && string.charAt(i)=='*' && string.charAt(i+1)=='*' && string.charAt(i+2)=='*' ){ string.deleteCharAt(i); string.deleteCharAt(i); string.deleteCharAt(i); string.deleteCharAt(i); string.deleteCharAt(i-1); i = i-2; }else if(i<=string.length() && string.charAt(i)=='*' && string.charAt(i+1)=='*' ){ string.deleteCharAt(i); string.deleteCharAt(i); string.deleteCharAt(i); string.deleteCharAt(i-1); i = i-2; }else if(i<=string.length() && string.charAt(i)=='*'){ string.deleteCharAt(i); string.deleteCharAt(i); string.deleteCharAt(i-1); i = i-2; } } res += string; return res; }
how can i do this better?Last edited by HearT.Hunt3r; 08-24-2011 at 02:09 AM.
- 08-24-2011, 01:16 AM #2
ACK!
Never mind I cannot read.
- 08-24-2011, 01:18 AM #3
The simplest way to achieve this would be to use indexOf and substring.
- 08-24-2011, 01:19 AM #4
And This is all exam:
Expected This Run
starOut("ab*cd") → "ad" "ad" OK
starOut("ab**cd") → "ad" "ad" OK
starOut("sm*eilly") → "silly" "silly" OK
starOut("sm*eil*ly") → "siy" "siy" OK
starOut("sm**eil*ly") → "siy" "siy" OK
starOut("sm***eil*ly") → "siy" "siy" OK
starOut("stringy*") → "string" "string" OK
starOut("*stringy") → "tringy" "tringy" OK
starOut("*str*in*gy") → "ty" "ty" OK
starOut("abc") → "abc" "abc" OK
starOut("a*bc") → "c" "c" OK
starOut("ab") → "ab" "ab" OK
starOut("a*b") → "" "" OK
starOut("a") → "a" "a" OK
starOut("a*") → "" "" OK
starOut("*a") → "" "" OK
starOut("*") → "" "" OK
starOut("") → "" "" OK
other test OK
- 08-24-2011, 01:22 AM #5
but how can i find all of the world by indexOf ?
- 08-24-2011, 01:36 AM #6
What? Huh? Please explain.
- 08-24-2011, 01:38 AM #7
we have like this for ex : "vahidxyvahidxyvahidxy"
how can find all xy from this by indexOf?
- 08-24-2011, 01:45 AM #8
How would you do it?
I mean with a pen and paper not using code.
- 08-24-2011, 01:47 AM #9
i donnu what did u said...anyway thx.
- 08-24-2011, 01:50 AM #10
:sigh:
How would you do something more than once?
- 08-24-2011, 01:58 AM #11
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
A couple of ideas:
- Nested loops: When you find a '*', do another loop to find the end of the '*'s. Then delete characters.
- Keep track of state: Store the index where you found the fist '*', and then continue going around the main loop until you find the last '*'. Then delete. But then you'll have to change the loop index, 'i', for it to come out right.
- Copy characters from a String to a StringBuilder: Copy non-'*' characters as you find them (except in some cases). Probably have to keep track of the "previous character" found/processed. (If previousChar != '*' and thisChar == '*' then delete the last char from the output.)
- 08-24-2011, 02:03 AM #12
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
- 08-24-2011, 02:05 AM #13
A single while loop is all that is needed. Inside the loop you need an if statement to handle the four different situations: * is first char, * is last char, * is followed by another *, default of single * in the middle. Each condition will need to do 1 or 2 substring calls.
- 08-24-2011, 02:13 AM #14
- 08-24-2011, 02:21 AM #15
OK simpler solution is to only add a char to a StringBuilder if it is not followed or preceded by a *.
- 08-24-2011, 02:24 AM #16
- 08-24-2011, 04:22 AM #17
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
Hmmm...
Well, I tried it this way, and I tried it with my suggestion #3 above. And I refactored heavily, relying on automated unit tests. Much as I like my heavily computer science automata theory based state machine approach, I think I like the code that I got from Junky's suggestion better: It's more understandable, and hence more maintainable.
- 08-24-2011, 03:12 PM #18
i got that how i can find all word in a string by indexOf ..

int v=0;
for(int i=0;i<str.length();i++){
v = str.indexOf(word, i);
i += v;
}
- 08-24-2011, 04:26 PM #19
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
The 'String.indexOf(char)' method can be used to find '*' character(s) in a string. So it could be used in this assignment.
However, the 'for' loop and 'charAt(i)' in your original posting will also work just as well. So I don't see a good reason for you to chase the 'indexOf' idea, unless you really want to.
- 09-18-2012, 09:17 AM #20
Member
- Join Date
- Sep 2012
- Posts
- 1
- Rep Power
- 0
Re: how can i do this so better?!
The code below uses charAt() within a for-loop. It handled the 20 test strings I threw at it.
Java Code:public String starOut(String str) { String result = ""; boolean delete = false; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '*') delete = true; else if (delete) delete = false; else if (i == str.length() - 1 || str.charAt(i + 1) != '*') result = result + str.charAt(i); } return result; }Last edited by PavelChovanec; 09-18-2012 at 09:19 AM.


3Likes
LinkBack URL
About LinkBacks

.gif)

Bookmarks