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'










share|improve this 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















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'










share|improve this 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













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'










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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













  • 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













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.



  1. Declare two int variables beforeIndex and afterIndex

  2. 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.

  3. 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 for afterIndex and break. (Assumption if before= '(' and after = ')' then for input = "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).

  4. 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;






share|improve this answer





























    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;






    share|improve this answer



























      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.



      1. Declare two int variables beforeIndex and afterIndex

      2. 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.

      3. 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 for afterIndex and break. (Assumption if before= '(' and after = ')' then for input = "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).

      4. 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;






      share|improve this answer


























        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.



        1. Declare two int variables beforeIndex and afterIndex

        2. 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.

        3. 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 for afterIndex and break. (Assumption if before= '(' and after = ')' then for input = "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).

        4. 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;






        share|improve this answer
























          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.



          1. Declare two int variables beforeIndex and afterIndex

          2. 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.

          3. 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 for afterIndex and break. (Assumption if before= '(' and after = ')' then for input = "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).

          4. 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;






          share|improve this answer














          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.



          1. Declare two int variables beforeIndex and afterIndex

          2. 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.

          3. 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 for afterIndex and break. (Assumption if before= '(' and after = ')' then for input = "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).

          4. 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;







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 17:48

























          answered Nov 10 at 13:30









          nits.kk

          3,29431739




          3,29431739






















              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;






              share|improve this answer
























                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;






                share|improve this answer






















                  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;






                  share|improve this answer












                  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;







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 13:27









                  J0ker98

                  1379




                  1379













                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3