-
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);
-
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;
}
-
I thought that all programs start with the main function and end when the main function ends, so where should i put ssFind?
-
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
}
-
if i put the prototype in my header file sstring.h will that have the same effect and putting it before main?
-
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.
-