string help plz

This is a discussion on string help plz within the C Programming forums, part of the General Programming Boards category; the ssFind is suppose to return the starting character position of toFind within src, if src does not contain toFind, ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    15

    string help plz

    the ssFind is suppose to return the starting character position of toFind within src, if src does not contain toFind, 0 is returned.

    AM i close or way off? Im suppose to be indexing based on 1, and im utterly confused why it doesn't compile. I know im a newb and any hints on whats wrong with it is greatly appreciated. Thanks!


    Code:
    #include <stdio.h>
    #include <string.h>
    #include "sstring.h"
    int main()
    {
    long ssFind(String *src, String *toFind);{
    
    	int i;
    	int j;
    	char a, b;
    
    	(src->data)[i];
    	(toFind->data)[j];
    
    
    	for (i = 1; i <(src->length); i++)
    		a = (src->data)[1];
    
    	for (j = 1;j<i; j++)
    		b = (toFind->data)[1];
    
    
    
    if(char *strstr(char *src,char *toFind))
    	return b;
    else 
           return 0;
    }
    
    }


    and the string.h file is this

    Code:
    #include <string.h>
    struct sstring {
    	size_t length;
    	size_t maxlength;
    	char *data;
    } ;
    
    typedef struct sstring String;
    String ........New(size_t len);
    void ........Free(String *s);
    String ........Check(String *s, size_t len);
    String ........Init(String *s, char *chrs);
    size_t ........Count(String *s, char *delims);

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    1. you can't nest functions like you did with main() and ssFind(). One function must finish before coding another.


    2. if you are allowed to use standard C string function strstr() then what is all that do-nothing code in ssFind()?
    Code:
    long ssFind(String *src, String *toFind)
    {
        long n = 0;
        char*p = strstr(src->data,toFind->data);
        if(p != NULL)
          n = (p - src->data);
       return n;
    }

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    I thought that all programs start with the main function and end when the main function ends, so where should i put ssFind?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    although main() is a required function, it is otherwise a normal function and has most of the same properties of any other function. You can put ssFind() either before or after main(), it doesn't really make a difference. If you put it after main() then you will need to prototype it before main.
    Code:
    long ssFind(String *src, String *toFind);
    
    int main(int argc, char* argv[])
    {
       // put your code here
    }
    
    long ssFind(String *src, String *toFind)
    {
        // put code here
    }

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    if i put the prototype in my header file sstring.h will that have the same effect and putting it before main?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. All a function prototype does is let your compiler know what the function you're calling takes for arguments and a return type. Without the function being defined before you use it, if you don't prototype it, your compiler doesn't know what it's supposed to take and return.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    thanks all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. String Verification?? Help plz!
    By Evangelic04 in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2004, 10:55 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21