A little explanation before I get to my copypasta....Each line in an array (already populated), parseLinks is called to determine if at any point in that line a quotation mark is found as one of the characters. If it is, I want to begin copying from the next letter, until the letter before a closing quotation mark. From there, I want to determine if the string is in-fact a link, which is where the compare comes in. Links are passed back to main, whereas an empty string is passed back if it isn't a link. (and a counter I have in main isn't iterated, so it skips that one and tries to re-write in that array block...but that's irrelevant)
I'm guessing my code is fucked up for the detection of the quotation mark(s), because I'm not getting any values sent back in the string.
string parseLinks(string str)
{
const int len = str.length();
string link = "";
bool quotes = false, islink = false;
string compare[5] = {".htm",".html",".php",".asp",".pdf"};
//Parse all quoted text
for (int i = 0; i != len; i++)
{
//Change bool if quote found
if (str[i] == '"')
{
if (quotes == false)
quotes = true;
else
quotes = false;
}
//If bool true, and char is not a quote, add to link string
if (quotes == true && str[i] != '"')
link += str[i];
}
//Discard non-link text
for (int i = 0; i < 5; i++)
{
if (link.compare(compare[i]) == 0)
islink = true;
}
//If not a link, empty return string
if (islink == false)
link = "";
return link;
}
Name:
Anonymous2011-09-20 1:49
//Parse all quoted text
int i, c=0;
for (i = 0; i <= len; i++)
{
//>> bool true, and char is not a quote, add to link string
if ((quotes == true) + ((int)str[i] != 34) == 2)
c++;
link[c] = str[i]; //??
//Change bool if quote found
if ((int)str[i] == 34)
quotes = !quotes;
}
Ok, so it's position of the start of compared string, and length of compared string, so I'd be looking for something like:
string comp (".php");
link.compare((comp.len() - 4), 4, comp);
...or something along those lines
Badabing! Figured it out, here is the working compare code I got:
//Discard non-link text
for (int i = 0; i < 5; i++)
{
if (link.compare((link.length() - compare[i].length()),compare[i].length(),compare[i]) == 0)
islink = true;
}
Welcome to the interweb
We got fun 'n' games
We got all the memes you want
Honey we know the memes
We are the people that can find
The Belair you may need
If you got the funny honey
We got your disease (AIDS)
True, but I hate when people have to type the namespace for every little fucking thing, instead of just listing the namespace and not having to do it everytime, because I can't be bothered to dig out that garbage
Name:
Anonymous2011-09-21 22:26
>>15
Look upon this man, /prog/. Look upon this man and weep for humanity.
>>16
I don't understand his post. Would you mind translating it to English?
Name:
Anonymous2011-09-22 2:46
What the hell are they teaching kids these days? Your algorithm is overly complicated and inefficient.
strchr your '"'. There's the start of the string you want. Then strchr for a second '"' from there and you got the end. The difference between the two, +1, is your length. Now you can use that to compare directly. None of that stupid bool bullshit, having to allocate memory for another string, and appending one character at a time to it.
Same here:
for (int i = 0; i < 5; i++)
{
if (link.compare(compare[i]) == 0)
islink = true;
}
//If not a link, empty return string
if (islink == false)
link = "";
return link;
How about returning the damn string as soon as you've found it? Then if the loop finished you KNOW you haven't found one so you can just return the empty string right there. No need to keep checking something since islink isn't going back to false once you set it.
You say "it's only 5 iterations, whatever". No. This is a weakness in your thinking. Always think of the simplest algorithm you can, and if it isn't obvious to you, you need to think more. Programmers stuck in your mindset are the reason software is turning to shit today --- because they can't see the obvious solution and resort to something overly complex.