Thread: Need help to fix function to be used in main

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    19

    Need help to fix function to be used in main

    I am trying to have my function return # of repeated letters in a strg. Ex. mississippi has 3:i,s,p(can't count repeats) I am getting no errors but can't figure out how to get the right answer. If anyone can help thanks.
    Code:
     
    #include <stdio.h>
    int repeat(char *ptr)
    { 
     int count=0,count2,i,n=26,k=0; 
     char x[]="abcdefghijklmnopqrstuvwxyz";     
     for(i=0; i<n; i++)
     {    
      count2=0;
      while(*ptr != '\0')
      {    
       if ((*ptr=x[i]))
        count2++;
       ptr++; k++;
       if (count2==2)
        count++;
      }
      ptr-=k;
     }
    return count;
    } 
    int main( void)
    {
     int e=0;
      char strg[]="mississippi";
      printf("before repeat: %d \n", e);
     e=repeat(strg);
     printf("after repeat: %d \n", e); 
    return 0;
    }

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Code:
    if ((*ptr=x[i]))
    There's your problem, you are assigning instead of comparing.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Ive toggled between = and ==. If i simplfy the code down just to count strg length with ==, it returns 0 with = it returns 11. Thanks for reply though.

  4. #4
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    That's because

    Code:
    if ((*ptr=x[i]))
        count2++;

    will always be true, so the counter will click over. You have to compare using ==. I think the bug is in the incrementing and subtraction of the pointer values.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    It is returning 0 even when i reduce it to:
    Code:
    if (*ptr==x[i])
    count++;
    *ptr++;
    The answer would be 11 but only return 11 when i use single.

  6. #6
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Code:
    int repeat(char *ptr)
    { 
     int count = 0, count2, i, n = 26, k = 0; 
     char *ref = ptr;
     char x[]="abcdefghijklmnopqrstuvwxyz";     
     
     for( i = 0; i < n; i++ )
     {    
      count2 = 0;
      
      while( *ptr != '\0' )
      {    
           if ( *ptr == x[i] )
           {
                count2++;
           }
           
           ptr++;
           k++;
           
           if ( count2 == 2 )
           {
                count++;
           }
      }
      
      ptr = ref;
     
     }
    return count;
    }

    There were issues with the subtraction of pointers. It's easier just to save a copy of the original. It's still buggy, but you should be able to move on from this problem.

    Items in red are things I changed - in blue, I removed.
    Last edited by bivhitscar; 04-28-2006 at 07:44 PM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    That last reply really helped me, Thanks alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM