Thread: write functions with pointers variables

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    6

    write functions with pointers variables

    Hi all,

    I have trouble with the below function:

    int suffix_searching(char*str, int len, char*suffix);

    The function copies the suffix of length len of string str into string suffix and returns 1 if len is smaller than or equal to the length of str; returns 0 without copying if len is larger than the length of str

    Thanks

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Fantastic, care elaborating?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Perhaps something like this or shed more light on the problem using code or pseudo-code.
    Code:
    int suffix_searching(char *str, int len, char *suffix)
    {
        int nc = 0;
    
        if (len <= strlen(str)) {
            while (*suffix++ = *str++) {
                if (++nc >= len)
                    break;
            }
            return 1;
        }
        return 0;
    }

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I posted code that does exactly this once... If you do as the link suggests you could actually end up stealing my code for your homework with no one being the wiser, but since that option is now on the table I am somehow doubting anyone will help you use the search thingy to find that post of mine.


    [edit]789502 for anyone who would even know what to do with that number.[/edit]
    Last edited by master5001; 10-24-2008 at 12:06 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    Hi all. Thanks for all the reply.
    What about this function char *substring(char *stra, char *strb);

    Function returns NUll when strb is not a substring of stra. On the other hand, when strb is a substring of stra, it returns a pointer pointing to the 1st occurrance of strb in stra.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *substring (char *string, char *sub) {
    	int stl=strlen(string), sbl=strlen(sub), i, n;
    	char chunk[sbl+1], *ptr;
    	for (i=0;i<stl;i++) {
    		if (sbl+i > stl) return NULL;
    		for (n=0;n<sbl;n++) chunk[n]=string[i+n];
    		chunk[n]=0;
    		if (strcmp(sub,chunk) == 0) {return (char*)string+i;}
    	}
    	return NULL;
    }
    
    int main() {
    	char string[64], sub[64], *ptr;
    	printf("Enter a string: ");
    	fgets(string,63,stdin);
    	printf("Enter a substring: ");
    	fgets(sub,63,stdin);
    	sub[strlen(sub)-1]=0;  // remove newline
    	ptr=substring(string,sub);
    	if (ptr != NULL) printf("%s\n",ptr);
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How could i write functions for these?
    By Matus in forum C Programming
    Replies: 3
    Last Post: 02-29-2008, 03:31 AM
  2. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  3. Naming variables, functions...
    By Ariod in forum Tech Board
    Replies: 9
    Last Post: 08-19-2003, 12:17 PM
  4. Variables do not equal functions!!!
    By me@burk. in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 06:24 AM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM