Thread: C function

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    17

    Question C function

    I wrote a c function. But not sure whether it is right. To be honest, I am newcomer for any programming.

    Q: Write a C function with the prototype

    int str_count(const char *source, const char *target);

    that returns the number of times the string target occurs within the string source. Both source and target are pointers to null terminated character arrays. If target cannot be found within source, the function returns 0, otherwise it returns the number of occurrences of target.

    The occurrences of target may overlap and the function should be case-insensitive. For example, it the source string is "abABaba" and the target string is "aba" the functionstr_count() should return 3.

    Soultion:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>



    int str_count(const char *source const char *target)
    {

    char source[12], target[4];
    int i, count=0;
    /*input source string*/
    printf("input source string");
    gets(source);
    printf("input target string");
    gets(target);


    for(i=0; i<12;i++)
    {
    if(source[i]=target[1]
    &&ources[i+1]=target[2]&&
    source[i+2]=target[3]&&souce[i+3]=target[4])
    { count=count+1;

    } return (count);
    else{
    return 0;
    }
    }


    }

    There is anybody help me check it. Thanks a lot.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    16
    One error I see is that your function is set to recieve the "target" and "source" varible when it's run but within it you redeclare them.You should probably declare them within main and pass them along to your function.
    Last edited by angelfly; 10-05-2001 at 05:16 PM.

  3. #3
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    I found a whole slew of errors in this program. 14 to be exact, and a few warnings. Well, here goes.

    You need a main function, every program must have a main function unless it is a header file to a driver that has the main function. Be sure to add this and do all of your input there, then call your str_count function.

    >if(source[i]=target[1]
    &&ources[i+1]=target[2]&&
    source[i+2]=target[3]&&souce[i+3]=target[4])

    Think of the = sign as meaning 'is now'. Is source[i] = target[1]? It is now! The = sign is used for assignment, you were trying to assign the value of target[1] to source[i] and this is illegal. Even if it weren't, the effect would be anything but what you wanted. Use == for comparisons.

    You also spelled your array name incorrectly not once in that line, but twice. Always double check your spelling and you will be saved a lot of errors.

    >return (count);
    Use this at the end of your function, not after the if statement. If you left it this way you would perform the if statement once and then leave the function. As it is you want the function to perform it's duties completely before returning and that means returning outside of the loop.

    Here's the same program with the necessary changes, I did not check to see if it worked properly, only compiled without errors or warnings.
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    
    int str_count(const char *source, const char *target) 
    {
      int i, count=0;
      for(i=0; i<12;i++) 
      { 
        if(source[i]==target[1] && source[i+1]==target[2] && source[i+2]==target[3] && source[i+3]==target[4]) 
    	{ 
    	  count=count+1; 
    	}  
        else
    	{ 
          return 0; 
    	} 
      }
      return (count);
    }
    
    int main() {
      char source[12], target[4]; 
      
      /*input source string*/ 
      printf("input source string"); 
      gets(source); 
      printf("input target string"); 
      gets(target);
    
      str_count(source, target);
    
      return 0;
    }
    pointer = NULL

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    4
    The problem your having is trying to over complicate your code, this is something everyone suffers from though i have written a program with a function to calculate the number of occurences


    /* count.c */
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int str_count(char *source_ptr, char *target_ptr);

    void main()
    {
    int count;

    count = str_count("abababa", "aba");

    printf("\n%d\n", count);

    return;
    }

    int str_count(char *source_ptr, char *target_ptr)
    {
    int size, count = 0;

    size = strlen(target_ptr);

    while(*source_ptr)
    {

    if(!strncmp(source_ptr, target_ptr, size))
    count++;

    source_ptr++;
    }
    return count;
    }

    The size of the strings can be of any length but you will need to change the return type if numbers get to big.

    if you dont get some of it let me know

    happy coding

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Isnt it redundant to check after reaching the
    strlen(source_ptr)-strlen(target_ptr)+1 position , it might be significant if strlen(source_ptr),strlen(target_ptr) are large

    Originally posted by Optical_Stinger
    The problem your having is trying to over complicate your code, this is something everyone suffers from though i have written a program with a function to calculate the number of occurences


    /* count.c */
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int str_count(char *source_ptr, char *target_ptr);

    void main()
    {
    int count;

    count = str_count("abababa", "aba");

    printf("\n%d\n", count);

    return;
    }

    int str_count(char *source_ptr, char *target_ptr)
    {
    int size, count = 0;

    size = strlen(target_ptr);

    while(*source_ptr)
    {

    if(!strncmp(source_ptr, target_ptr, size))
    count++;

    source_ptr++;
    }
    return count;
    }

    The size of the strings can be of any length but you will need to change the return type if numbers get to big.

    if you dont get some of it let me know

    happy coding

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    4

    reply to pinko_liberal

    the function may be more efficiently coded, but if so why don't you post an alternative. My function works, if it is a few hundreds of a second slower so what. The Quality of a program is it's ability to be maintained, an easy to maintain program is better than a program which runs slightly faster.

    For example suppose you came along a year after the program had been written, would the original example posted by Tombear be obvious in it's intention, if not the program has failed and you would spend hours trying to figure out the result.

    Pinko_liberal The best program is one which any other programmer can understand(within reason). I accept my program isn't as efficient as it good be but as you offer no alternative I can't take you seriously. Tom bear obviously isn't an experienced programmer(no offense Tom bear) and such improvements will be benificial for him to make himself , after all the journey through coding isn't an easy one. I don't wish to offend anyone but if I can help someone to expand their thought's I'm happy.

  7. #7
    Unregistered
    Guest

    Re: reply to pinko_liberal

    You are taking offense where none was intended , I was not questioning your capabilities , I apologize if my post seemed offensive , I was just trying to help , anyway in future I'll try my best to stay away from threads you initiate and I'll definitely never comment on your code.
    Let me explain what I meant this time , I am taking the liberty to tweak your code a little .


    int str_count(char *source_ptr, char *target_ptr)
    {
    int i,len,size, count = 0;

    len=strlen(source_ptr);
    size = strlen(target_ptr);

    for(i=0;i<=len-size;i++,source_ptr++)
    {
    if(strncmp(source_ptr, target_ptr,size)==0)
    count++;
    }
    return count;
    }


    If you are searching for a pattern of length 50 in a string of length 100 , it saves you a lot of redundant comparisons as calls to strncmp are cut almost by half.
    Does the above code seem extremely difficult to maintain and the logic too convoluted ?


    Originally posted by Optical_Stinger
    the function may be more efficiently coded, but if so why don't you post an alternative. My function works, if it is a few hundreds of a second slower so what. The Quality of a program is it's ability to be maintained, an easy to maintain program is better than a program which runs slightly faster.

    For example suppose you came along a year after the program had been written, would the original example posted by Tombear be obvious in it's intention, if not the program has failed and you would spend hours trying to figure out the result.

    Pinko_liberal The best program is one which any other programmer can understand(within reason). I accept my program isn't as efficient as it good be but as you offer no alternative I can't take you seriously. Tom bear obviously isn't an experienced programmer(no offense Tom bear) and such improvements will be benificial for him to make himself , after all the journey through coding isn't an easy one. I don't wish to offend anyone but if I can help someone to expand their thought's I'm happy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM