Check if char comes before another char [closed]
up vote
-5
down vote
favorite
How can you check whether a specific char comes before another char using a for-loop?
E.g. the function should return false if ')' comes before '('. So the following strings would be false: ')abc', 'a)abc(', ')abc'
java string for-loop if-statement char
closed as too broad by T.J. Crowder, Nicholas K, GhostCat, Stephen C, nullpointer Nov 10 at 13:39
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-5
down vote
favorite
How can you check whether a specific char comes before another char using a for-loop?
E.g. the function should return false if ')' comes before '('. So the following strings would be false: ')abc', 'a)abc(', ')abc'
java string for-loop if-statement char
closed as too broad by T.J. Crowder, Nicholas K, GhostCat, Stephen C, nullpointer Nov 10 at 13:39
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
Have you tried anything to tackle the problem?
– Nicholas K
Nov 10 at 13:16
3
Hint: use String.indexOf()
– NiVeR
Nov 10 at 13:17
you can create some boolean flag variable to check whether '(' is already encountered or not, based on it you can build logic for checking ')' using flag. If multiple chars needs to be checked try using stack.
– Hrudayanath
Nov 10 at 13:22
add a comment |
up vote
-5
down vote
favorite
up vote
-5
down vote
favorite
How can you check whether a specific char comes before another char using a for-loop?
E.g. the function should return false if ')' comes before '('. So the following strings would be false: ')abc', 'a)abc(', ')abc'
java string for-loop if-statement char
How can you check whether a specific char comes before another char using a for-loop?
E.g. the function should return false if ')' comes before '('. So the following strings would be false: ')abc', 'a)abc(', ')abc'
java string for-loop if-statement char
java string for-loop if-statement char
asked Nov 10 at 13:15
Lorenz Wöhr
3362818
3362818
closed as too broad by T.J. Crowder, Nicholas K, GhostCat, Stephen C, nullpointer Nov 10 at 13:39
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by T.J. Crowder, Nicholas K, GhostCat, Stephen C, nullpointer Nov 10 at 13:39
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
Have you tried anything to tackle the problem?
– Nicholas K
Nov 10 at 13:16
3
Hint: use String.indexOf()
– NiVeR
Nov 10 at 13:17
you can create some boolean flag variable to check whether '(' is already encountered or not, based on it you can build logic for checking ')' using flag. If multiple chars needs to be checked try using stack.
– Hrudayanath
Nov 10 at 13:22
add a comment |
2
Have you tried anything to tackle the problem?
– Nicholas K
Nov 10 at 13:16
3
Hint: use String.indexOf()
– NiVeR
Nov 10 at 13:17
you can create some boolean flag variable to check whether '(' is already encountered or not, based on it you can build logic for checking ')' using flag. If multiple chars needs to be checked try using stack.
– Hrudayanath
Nov 10 at 13:22
2
2
Have you tried anything to tackle the problem?
– Nicholas K
Nov 10 at 13:16
Have you tried anything to tackle the problem?
– Nicholas K
Nov 10 at 13:16
3
3
Hint: use String.indexOf()
– NiVeR
Nov 10 at 13:17
Hint: use String.indexOf()
– NiVeR
Nov 10 at 13:17
you can create some boolean flag variable to check whether '(' is already encountered or not, based on it you can build logic for checking ')' using flag. If multiple chars needs to be checked try using stack.
– Hrudayanath
Nov 10 at 13:22
you can create some boolean flag variable to check whether '(' is already encountered or not, based on it you can build logic for checking ')' using flag. If multiple chars needs to be checked try using stack.
– Hrudayanath
Nov 10 at 13:22
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Lets define a function to resolve your problem
Note that below solution uses For loop as mentioned in the question. String.java
has a method indexOf
, it can directly be used to obtain the first occurance of the character which is needed to occur before and the character which is needed to occur after. Once both the index are known a simple comparison will give the result.
Function defination:
public boolean isValid( char before, char after , String input);
Next lets see the implementation.
- Declare two int variables
beforeIndex
andafterIndex
- In a for loop iterate the input string and for each char check if the character is equal to
before
, if so update the value of beforeIndex with the currwnt index and break the loop. - In an another for loop iterate the input string and for each character check if the current character equals to
after
. If so then update the value forafterIndex
and break. (Assumption ifbefore= '('
andafter = ')'
then forinput = "ab)d(e)"
function returns false, if this assumption is wrong then let the loop for step 3 continue to its completion and do not break). - return
afterIndex > beforeIndex
Below is the function implementing above steps
public boolean isValid( char before, char after , String input)
int beforeIndex = 0, afterIndex = 0;
for( int i = 0; i < input.length(); i++)
if(input.charAt(i) == before)
beforeIndex = i;
break;
for( int i = 0; i < input.length(); i++)
if(input.charAt(i) == after)
afterIndex = i;
break;
return afterIndex > beforeIndex;
add a comment |
up vote
0
down vote
You can just loop on the String, when you encounter the ‘)’ set a boolean to true, then on the next iteration, if the boolean is true check if the current char equals to your last char, if so feturn true else set the boolean to false and keep going. Here’s a method that would do the job:
public boolean checkChar(String first, String last, String s)
boolean check = false;
for(int i = 0; i < s.length(); i++)
if (check)
if(s.charAt(i).equals(last)) return true;
else check = false;
else if(s.charAt(i).equals(first) check = true;
return false;
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Lets define a function to resolve your problem
Note that below solution uses For loop as mentioned in the question. String.java
has a method indexOf
, it can directly be used to obtain the first occurance of the character which is needed to occur before and the character which is needed to occur after. Once both the index are known a simple comparison will give the result.
Function defination:
public boolean isValid( char before, char after , String input);
Next lets see the implementation.
- Declare two int variables
beforeIndex
andafterIndex
- In a for loop iterate the input string and for each char check if the character is equal to
before
, if so update the value of beforeIndex with the currwnt index and break the loop. - In an another for loop iterate the input string and for each character check if the current character equals to
after
. If so then update the value forafterIndex
and break. (Assumption ifbefore= '('
andafter = ')'
then forinput = "ab)d(e)"
function returns false, if this assumption is wrong then let the loop for step 3 continue to its completion and do not break). - return
afterIndex > beforeIndex
Below is the function implementing above steps
public boolean isValid( char before, char after , String input)
int beforeIndex = 0, afterIndex = 0;
for( int i = 0; i < input.length(); i++)
if(input.charAt(i) == before)
beforeIndex = i;
break;
for( int i = 0; i < input.length(); i++)
if(input.charAt(i) == after)
afterIndex = i;
break;
return afterIndex > beforeIndex;
add a comment |
up vote
1
down vote
accepted
Lets define a function to resolve your problem
Note that below solution uses For loop as mentioned in the question. String.java
has a method indexOf
, it can directly be used to obtain the first occurance of the character which is needed to occur before and the character which is needed to occur after. Once both the index are known a simple comparison will give the result.
Function defination:
public boolean isValid( char before, char after , String input);
Next lets see the implementation.
- Declare two int variables
beforeIndex
andafterIndex
- In a for loop iterate the input string and for each char check if the character is equal to
before
, if so update the value of beforeIndex with the currwnt index and break the loop. - In an another for loop iterate the input string and for each character check if the current character equals to
after
. If so then update the value forafterIndex
and break. (Assumption ifbefore= '('
andafter = ')'
then forinput = "ab)d(e)"
function returns false, if this assumption is wrong then let the loop for step 3 continue to its completion and do not break). - return
afterIndex > beforeIndex
Below is the function implementing above steps
public boolean isValid( char before, char after , String input)
int beforeIndex = 0, afterIndex = 0;
for( int i = 0; i < input.length(); i++)
if(input.charAt(i) == before)
beforeIndex = i;
break;
for( int i = 0; i < input.length(); i++)
if(input.charAt(i) == after)
afterIndex = i;
break;
return afterIndex > beforeIndex;
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Lets define a function to resolve your problem
Note that below solution uses For loop as mentioned in the question. String.java
has a method indexOf
, it can directly be used to obtain the first occurance of the character which is needed to occur before and the character which is needed to occur after. Once both the index are known a simple comparison will give the result.
Function defination:
public boolean isValid( char before, char after , String input);
Next lets see the implementation.
- Declare two int variables
beforeIndex
andafterIndex
- In a for loop iterate the input string and for each char check if the character is equal to
before
, if so update the value of beforeIndex with the currwnt index and break the loop. - In an another for loop iterate the input string and for each character check if the current character equals to
after
. If so then update the value forafterIndex
and break. (Assumption ifbefore= '('
andafter = ')'
then forinput = "ab)d(e)"
function returns false, if this assumption is wrong then let the loop for step 3 continue to its completion and do not break). - return
afterIndex > beforeIndex
Below is the function implementing above steps
public boolean isValid( char before, char after , String input)
int beforeIndex = 0, afterIndex = 0;
for( int i = 0; i < input.length(); i++)
if(input.charAt(i) == before)
beforeIndex = i;
break;
for( int i = 0; i < input.length(); i++)
if(input.charAt(i) == after)
afterIndex = i;
break;
return afterIndex > beforeIndex;
Lets define a function to resolve your problem
Note that below solution uses For loop as mentioned in the question. String.java
has a method indexOf
, it can directly be used to obtain the first occurance of the character which is needed to occur before and the character which is needed to occur after. Once both the index are known a simple comparison will give the result.
Function defination:
public boolean isValid( char before, char after , String input);
Next lets see the implementation.
- Declare two int variables
beforeIndex
andafterIndex
- In a for loop iterate the input string and for each char check if the character is equal to
before
, if so update the value of beforeIndex with the currwnt index and break the loop. - In an another for loop iterate the input string and for each character check if the current character equals to
after
. If so then update the value forafterIndex
and break. (Assumption ifbefore= '('
andafter = ')'
then forinput = "ab)d(e)"
function returns false, if this assumption is wrong then let the loop for step 3 continue to its completion and do not break). - return
afterIndex > beforeIndex
Below is the function implementing above steps
public boolean isValid( char before, char after , String input)
int beforeIndex = 0, afterIndex = 0;
for( int i = 0; i < input.length(); i++)
if(input.charAt(i) == before)
beforeIndex = i;
break;
for( int i = 0; i < input.length(); i++)
if(input.charAt(i) == after)
afterIndex = i;
break;
return afterIndex > beforeIndex;
edited Nov 10 at 17:48
answered Nov 10 at 13:30
nits.kk
3,29431739
3,29431739
add a comment |
add a comment |
up vote
0
down vote
You can just loop on the String, when you encounter the ‘)’ set a boolean to true, then on the next iteration, if the boolean is true check if the current char equals to your last char, if so feturn true else set the boolean to false and keep going. Here’s a method that would do the job:
public boolean checkChar(String first, String last, String s)
boolean check = false;
for(int i = 0; i < s.length(); i++)
if (check)
if(s.charAt(i).equals(last)) return true;
else check = false;
else if(s.charAt(i).equals(first) check = true;
return false;
add a comment |
up vote
0
down vote
You can just loop on the String, when you encounter the ‘)’ set a boolean to true, then on the next iteration, if the boolean is true check if the current char equals to your last char, if so feturn true else set the boolean to false and keep going. Here’s a method that would do the job:
public boolean checkChar(String first, String last, String s)
boolean check = false;
for(int i = 0; i < s.length(); i++)
if (check)
if(s.charAt(i).equals(last)) return true;
else check = false;
else if(s.charAt(i).equals(first) check = true;
return false;
add a comment |
up vote
0
down vote
up vote
0
down vote
You can just loop on the String, when you encounter the ‘)’ set a boolean to true, then on the next iteration, if the boolean is true check if the current char equals to your last char, if so feturn true else set the boolean to false and keep going. Here’s a method that would do the job:
public boolean checkChar(String first, String last, String s)
boolean check = false;
for(int i = 0; i < s.length(); i++)
if (check)
if(s.charAt(i).equals(last)) return true;
else check = false;
else if(s.charAt(i).equals(first) check = true;
return false;
You can just loop on the String, when you encounter the ‘)’ set a boolean to true, then on the next iteration, if the boolean is true check if the current char equals to your last char, if so feturn true else set the boolean to false and keep going. Here’s a method that would do the job:
public boolean checkChar(String first, String last, String s)
boolean check = false;
for(int i = 0; i < s.length(); i++)
if (check)
if(s.charAt(i).equals(last)) return true;
else check = false;
else if(s.charAt(i).equals(first) check = true;
return false;
answered Nov 10 at 13:27
J0ker98
1379
1379
add a comment |
add a comment |
2
Have you tried anything to tackle the problem?
– Nicholas K
Nov 10 at 13:16
3
Hint: use String.indexOf()
– NiVeR
Nov 10 at 13:17
you can create some boolean flag variable to check whether '(' is already encountered or not, based on it you can build logic for checking ')' using flag. If multiple chars needs to be checked try using stack.
– Hrudayanath
Nov 10 at 13:22