14 Replies Latest reply: Jan 3, 2008 10:18 AM by 807601 RSS

    how count the number of substrings in a string

    807601
      Hi all,

      I wonder if someone can help me and point out where I'm going wrong.


      I want to count the number of substring starting with 'd' and ending with 'e' in a string supplied by a user.

      currently my code is only counting the number of 'd' and 'e' characters in the string.
      {
      
          public static void main (String [] args) 
          
          {
               Scanner input = new Scanner (System.in);
               
               
               String mstring;// string variable that the user will enter
               
               int i = 0; // upstep variable
               
               int count = 0; // int variable to count the number of substrings
               
               System.out.print("Enter a string: ");
               
               
               mstring = input.nextLine();
               
               
               while (i != mstring.length())
                    
                    {
                                            
                         if(mstring.charAt(i)=='d'){count++;}
                         
                         else if (mstring.charAt(i)=='e'){count++;}
                              
                         
                         i++;
                    }
               
               System.out.println("The total d e substrings is " + count);
               
          }
          
          
      }
      For example if the user enters adbedeaadffe the total number of substrings should be 5 but I'm getting 6 as the program is just counting the number of times it comes across 'd' and 'e'

      Can anyone shed some light on this for me thanks.
        • 1. Re: how count the number of substrings in a string
          807601
          Hi all I had a good few replies to this question but they seem to have been removed from the system for some reason.

          I've changed my code slightly and it works but only for one round of the guard.
                         
                         Scanner input = new Scanner (System.in)
                   
                   String mstring;// main string variable that the user will enter
                   
                   int i = 0;  // upstep variable
                   
                   int count = 0; // int variable to count the number of substrings
               System.out.print("Enter a string: ");
                   
                   mstring = input.nextLine();
                   
                   int y =mstring.length();
                   
                   while (i != mstring.length() && y !=0)
                        
                   {
                             
                   if (mstring.charAt(i) == 'd' && mstring.charAt(y-1)=='e'){count = count+1;}
                             
                   y--;
                   i++;
                             
                   }
                   System.out.println("The total a b substrings is " + (count));
          The above code will only print out one substring of adbedeaadffe but that is not what I need, I need to be able to print out all of the substrings.

          Can anyone tell me where I'm going wrong.


          Thanks in advance
          • 2. Re: how count the number of substrings in a string
            800282
            Could you give some example in- and output?
            What substrings should "dffdge" produce? The substrings "dffdge" and "dge", or just one of them?
            And what wrong output does your code actually produce?
            • 3. Re: how count the number of substrings in a string
              807601
              Hope this code fragment wl solve ur purpose...

              String str="adbedeaadffe";
                        int count=0;
                        int n=0;
                        System.out.println(str.length());
                        for(int j=0;j<str.length();j++)
                        {

                                  if(str.charAt(j)=='d')
                                  {
              j++;
                                       n=j;
                                       while(j<str.length())
                                       {
                                       if(str.charAt(j)=='e')
                                       count++;
                                       j++;
                                       }
                                       j=n;
                                  }

                             }
              • 4. Re: how count the number of substrings in a string
                807601
                Hi all,

                Sorry about the lack of information. The system should count the number of substrings that it finds during the loop.

                Unfortunatly I'm stuck using while and if statements for this problem so I can't use for as suggested above but I'm sure that the code suggessted by Brightning_with_Sun... would work I just need to know how to write it using while and if

                Edited by: Irish-Student on Jan 3, 2008 1:45 PM
                • 5. Re: how count the number of substrings in a string
                  791266
                  Irish-Student wrote:
                  Hi all,

                  Sorry about the lack of information. The system should count the number of substrings that it finds during the loop.

                  Unfortunatly I'm stuck using while and if statements for this problem so I can't use for as suggested above but I'm sure that the code suggessted by Brightning_with_Sun... would work I just need to know how to write it using while and if

                  Edited by: Irish-Student on Jan 3, 2008 1:45 PM
                  A for loop can always be written as a while loop (the other way around is also true)
                  • 6. Re: how count the number of substrings in a string
                    800282
                    Irish-Student wrote:
                    Hi all,

                    Sorry about the lack of information. The system should count the number of substrings that it finds during the loop.

                    Unfortunatly I'm stuck using while and if statements for this problem so I can't use for as suggested above but I'm sure that the code suggessted by Brightning_with_Sun... would work I just need to know how to write it using while and if

                    Edited by: Irish-Student on Jan 3, 2008 1:45 PM
                    From the four questions I asked in my first reply to you, you didn't answer one!
                    • 7. Re: how count the number of substrings in a string
                      807601
                      Irish-Student wrote:
                      For example if the user enters adbedeaadffe the total number of substrings should be 5 but I'm getting 6 as the program is just counting the number of times it comes across 'd' and 'e'
                      Shouldn't this be 6?

                      dbe
                      dbede
                      dbedeaadffe
                      de
                      deaadffe
                      dffe

                      You should clarify what you're asking for, a chara! :)

                      Illu
                      • 8. Re: how count the number of substrings in a string
                        807601
                        prometheuzz I did reply to you and but for some reason all replies I've got to this question have been wiped from the system when I logged on this morning and I am not getting any emails telling me that replies have been made to my post either.

                        I mean no offence and am grateful for any help I receive from everyone here.
                        • 9. Re: how count the number of substrings in a string
                          807601
                          Illu your right and I'm sorry but dying with the flu here and got to get this done so apologies for any mistakes.
                          • 10. Re: how count the number of substrings in a string
                            807601
                            No probs, just trying to figure out what exactly you wanted. Try this and let me know if it works.
                            public static void main(String[] args) {
                                      String str="adbedeaadffe";
                                      
                                      int loopCount = 0;
                                      int innerLoopCount = 0;
                                      
                                      int dIndex = 0;
                                      
                                      int result = 0;
                                      
                                      while(loopCount < str.length()){
                                           if(str.charAt(loopCount) == 'd'){
                                                dIndex = loopCount;
                                                
                                                while(innerLoopCount < str.length()){
                                                     if(str.charAt(innerLoopCount) == 'e' && innerLoopCount > dIndex){
                                                          result++;
                                                     }
                                                     
                                                     innerLoopCount++;
                                                }
                                                innerLoopCount = 0;
                                           }
                                           
                                           loopCount++;
                                      }
                                      
                                      System.out.println(result);
                                 }
                            Illu
                            • 11. Re: how count the number of substrings in a string
                              800282
                              Irish-Student wrote:
                              prometheuzz I did reply to you and but for some reason all replies I've got to this question have been wiped from the system when I logged on this morning and I am not getting any emails telling me that replies have been made to my post either.

                              I mean no offence and am grateful for any help I receive from everyone here.
                              Err, I am talking about reply #2 which I posted this afternoon. I can't imagine you responded to that post.
                              No matter though, I saw some doofus already posted a copy-and-paste solution...
                              • 12. Re: how count the number of substrings in a string
                                807601
                                Hi all,

                                Thanks for the replies got it sorted now.

                                Thanks again for all the help

                                Slan.
                                • 13. Re: how count the number of substrings in a string
                                  807601
                                  Hi prometheuzz,

                                  I actually did reply but didn't mention you by name no disrespect was intended just sick here so not my usual self sorry
                                  • 14. Re: how count the number of substrings in a string
                                    800282
                                    Irish-Student wrote:
                                    Hi prometheuzz,

                                    I actually did reply but didn't mention you by name
                                    That could then only be your reply #4. Nowhere in that reply is an answer to one of my questions asked in reply #2.
                                    \\
                                    \\
                                    no disrespect was intended just sick here so not my usual self sorry
                                    No problem at all, I just find it strange that someone seeking help does not answer the questions a (possible) helper asks him/her.

                                    Best of luck anyway and hope you feel better soon.