What's going on? Help on this please..

This is a discussion on What's going on? Help on this please.. within the C Programming forums, part of the General Programming Boards category; Code: for(;concat[a] != '

Thread: What's going on? Help on this please..

';a++){ for(;concat[b] != '

Thread: What's going on? Help on this please..

';b++){ if(concat[a]==concat[b+2] && concat[a+1]==concat[b+3]) { count++; if(count>count2){ count2=count; count=1; printf(" %c%c", concat[b+2], concat[b+3]); printf(" ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    54

    What's going on? Help on this please..

    Code:
         for(;concat[a] != '\0';a++){
    		for(;concat[b] != '\0';b++){
                              if(concat[a]==concat[b+2] && concat[a+1]==concat[b+3]) {
    				count++;
    				if(count>count2){
    					count2=count;
    					count=1;
    					printf(" %c%c", concat[b+2], concat[b+3]);
    					printf(" %d",count2);
    				}
    				else if(count==count2){
    					count=1;
    					printf(" %c%c", concat[b+2], concat[b+3]);
    					printf(" %d",count2);
    				}
    			}
    		}
           }
    I'm trying to print out the most consecutive pairs. Please Help.

    Terribly sorry, its just a method from a part of a program. I'll post an example.

    Input : This is another test (Another method has been placed to ignore the white space and case).
    Output: (nothing happened).

    Expected output to print:
    th 2
    is 2

    I'm trying to place a check for the first pair to compare with the next pair and next till the end. It goes on until the first pair reaches the end.
    Last edited by DarrenY; 03-16-2005 at 11:14 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code fragments are wonderfully lacking in information -- care to provide any context or should we just try to read your mind?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    what horrible indentation of the code...please indent it properly, i doubt anyone will read it the way it is now.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Sometimes edit, sometimes reply.
    Code:
    #include<stdio.h>
    
    void foo(const char *concat)
    {
    #if 0 /* dangling code presumed to be part of 'foo' */
       // what is concat, really?
       // what is count?
       // what is count2?
       // what is a?
       // what is b?
       // do you realize that a+1 might be outside of concat[]?
       // do you realize that b+2 might be outside of concat[]?
       // do you realize that b+3 might be outside of concat[]?
       for ( ;concat[a] != '\0';a++ )
       {
          for ( ;concat[b] != '\0';b++ )
          {
             if ( concat[a]==concat[b+2] && concat[a+1]==concat[b+3] )
             {
                count++;
                if ( count>count2 )
                {
                   count2=count;
                   count=1;
                   printf(" %c%c", concat[b+2], concat[b+3]);
                   printf(" %d",count2);
                }
                else if ( count==count2 )
                {
                   count=1;
                   printf(" %c%c", concat[b+2], concat[b+3]);
                   printf(" %d",count2);
                }
             }
          }
       }
    #endif
    }
    
    int main(void)
    {
       foo("th");
       return 0;    
    }
    [edit]
    Code:
    #include <stdio.h>
    
    void foo(const char *text)
    {
       int i, j, consecutive;
       for ( i = 0; text[i] != '\0'; i += j )
       {
          consecutive = 1;
          for ( j = 1; text[j + i] == text[i]; ++j )
          {
             ++consecutive;
          }
          printf("so far, '%c' has %d consecutive\n", text[i], consecutive);
       }
    }
    
    int main(void)
    {
       foo("iii  aaammm   cccooonnnffffffuuuuuuuuussssssssssseeeeeeeeedddd");
       return 0;    
    }
    
    /* my output
    so far, 'i' has 3
    so far, ' ' has 2
    so far, 'a' has 3
    so far, 'm' has 3
    so far, ' ' has 3
    so far, 'c' has 3
    so far, 'o' has 3
    so far, 'n' has 3
    so far, 'f' has 6
    so far, 'u' has 9
    so far, 's' has 11
    so far, 'e' has 9
    so far, 'd' has 4
    */
    Last edited by Dave_Sinkula; 03-16-2005 at 11:44 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    Thanks Dave_Sinkula! It works now. Apparently your method has sparked an idea out from my mind. However, I do apologise for my post as it was my first time. Thanks! Cheers!