//Tests if the C string str starts with the specified prefix, the C string prefix.
bool startsWith(constchar* str, constchar* prefix)
{
//just have to check once if first letter of str[0] == prefex, if so then true else false
unsignedint i = prefix;
if (str[0] == prefix)
returntrue;
//default return is false
returnfalse;
}
(str[0] == prefix)str[0] is a char. prefix is a pointer - a memory address. You are trying to compare a char and a memory address. This makes no sense.
//Tests if the C string str starts with the specified prefix, the C string prefix.
bool startsWith(constchar* str, constchar* prefix)
{
constchar *pos = strstr(str, prefix);
return pos == str;
}
Repeater, yes i know trying to get a way around that, this is my first assignment in over 6 years. (we didn't even go over pointers, but i do know a little about it, it's an address in memory right?)
The function strstr returns a pointer to the beginning of the substring prefix in str.
Then you compare this beginning with the start of str.
If they are equal the start of str is the start of pos.
BTW. It would be easier to std::string instead of this old C stuff.
yes. If memory were a huge array, a pointer is the index into it.
strstr searches one string to see if it is inside another, eg abcd, ab, yes, ab is in abcd . If not inside it returns 0 (null pointer in C, these are C routines). If it is inside, it hands you the location of 'a' in my example, the starting point of the first place it found the substring you seek.
another way to do it is memcmp, and compare the first N bytes against a constant or a variable with the prefix, etc.
if you want to use C-strings, you really, really need to understand pointers and arrays well. That is what they ARE.
bool startsWith(const char* str, const char* prefix)
{
//
char pre = *prefix;
if (str == pre)
return true;
//default return is false
return false;
}
//but the problem is, i cannot make pre into an array (pre[15] to hold words), it only holds one letter and not the entire prefix.
we aren't allowed to do memcom functions or strstr functions yet unfortunately.
Used for loops in previous functions
ok, goodness. I think this covers everything except null pointers passed
into it. Hopefully this is all correct; ppl been confusing me makin me lern
c++ strings :P
bool startsWith(constchar* str, constchar* prefix)
{
int px = 0;
int sx = 0;
while(str[sx++]); //my teacher is not letting me code version of strlen
while(prefix[px++]);
if(px > sx) returnfalse;
px = 0;
while(prefix[px]) //my teacher is not letting me code version of memcmp
{
if(prefix[px] != str[px])
returnfalse;
px++;
}
returntrue;
}
I don't usually like to do too much homework for people but I make an exception if they won't LET you do stuff. You should be able to do anything you can learn and get compiled and running.