|
-
Jul 23rd, 2012, 11:39 AM
#1
[RESOLVED] How to deal with a string array in C++
I've got this code - and I cannot seem to get it to work...
Code:
__declspec(dllexport) int firstKeyRegions(char *strSearch, char **keywords, int nKeywords, int *smarkers, int *emarkers) {
int i = 0, j = 0, k = 0, a = 0;
int keywordlen = 0;
int lenSearch = strlen(strSearch);
bool gotSpace = false; //, inMatch = false, gotMatch = false, doNext = true;
int wordcnt = 0;
int headers = 0, headerse = 0;
int titles = 0, titlee = 0;
for(i = 0; i < lenSearch; i++) // Step through the whole text
{
j = strSearch[i];
gotSpace = (j == 32);
if(gotSpace) {
wordcnt += 1;
}
}
//string kw;
for(k = 0; k < nKeywords; k++) // Step through the keywords array
{
if (keywords[k] == "wordcnt")
{
smarkers[k] = wordcnt;
}
//{
// switch("xxx")
// {
// case 'wordcnt':
// smarkers[k] = wordcnt;
// break;
// case 'header':
// if (headers != 0)
// {
// smarkers[k] = headers;
// emarkers[k] = hearere;
// }
// break;
// case 'title':
// if (titles != 0)
// {
// smarkers[k] = titles;
// emarkers[k] = titlee;
// }
// break;
// }
}
return -1; // Return something
}
and my keywords array looks like this
Code:
?keywords[0]
0x0668e620 "wordcnt"
?keywords[1]
0x066849e8 "header"
?keywords[2]
0x0668e698 "title"
How do I work with this in C++???
This is called from VB like this
Code:
Dim rKeyTerms As String() = {"wordcnt", "header", "title"}
Dim kcValue As Integer = rKeyTerms.Count
Dim rKeyStart(kcValue - 1) As Integer
Dim rKeyEnd(kcValue - 1) As Integer
For i As Integer = 0 To kcValue - 1
rKeyStart(i) = -1
rKeyEnd(i) = -1
Next
Dim x As Object = firstKeyRegions(allText, rKeyTerms, kcValue, rKeyStart, rKeyEnd)
-
Jul 23rd, 2012, 01:01 PM
#2
Re: How to deal with a string array in C++
btw - this is the IF statement that won't work
if (keywords[k] == "wordcnt")
How do I look at keywords[] and see if the string is "wordcnt"?
-
Jul 26th, 2012, 04:10 AM
#3
Re: How to deal with a string array in C++
Ok - I think I know how to attack this - just wanted to post it here to make sure I was not re-inventing the wheel.
I believe I need to write a function that will pass in the reference to the array slot - which is really just the first character of the string - "w", for instance with "wordcnt" in the keywords[] array...
Code:
?keywords[0]
0x0668e620 "wordcnt"
?keywords[1]
0x066849e8 "header"
?keywords[2]
0x0668e698 "title"
The second argument for that function will be the "string" to match on - although I have no idea how to even make a function in C++ let alone pass in "wordcnt" or "xyz" as the second argument...
Then once in that function it's a silly loop to see if the reference passed in matches the "string" passed as the second argument.
Do I really need to write my own function to do such a task in C++.
If so - any help on how to code up the syntax of the function and the call itself?
-
Jul 26th, 2012, 04:45 AM
#4
Re: How to deal with a string array in C++
I wrote this function
Code:
bool isValue(char *strLocation, const char *checkValue) {
int lenLocation = strlen(strLocation);
int lenValue = strlen(checkValue);
int i = 0;
if (lenLocation != lenValue) {return false;}
for(i = 0; i <= lenLocation; i++)
{
if (strLocation[i] != checkValue[i]) {return false;}
}
return true;
}
and I tested it with a call like this - in the debugger...
Code:
gotValue = isValue(keywords[0], "wordcnt");
Seems to work - I stepped through it and all looked fine to me.
Is this the correct way to accomplish something like this?
Now I can loop through the keywords[] array and I guess put up a SWITCH TRUE statement and CASE each IsValue constant...
Is there anyone on this C++ forum??
-
Jul 28th, 2012, 05:28 PM
#5
Re: How to deal with a string array in C++
Yeah comparing the character arrays like this:
Code:
if (keywords[k] == "wordcnt")
{
}
will only compare the memory addresses of the character arrays and not the contents of the arrays.
There are plenty of ways to do it really. You could construct STL strings from your char arrays and use its equality operator like so:
Code:
if(std::string(keywords[k]) == "wordcnt")
{
}
but personally I find that it is a bit overkill to create std::string instances just for string comparison. Unless you find other needs to be using std::strings in your code I'd go for strcmp:
Code:
if(strcmp(keywords[k], "wordcnt") == 0)
{
// strings are equal
}
-
Jul 28th, 2012, 05:31 PM
#6
Re: How to deal with a string array in C++
Thank you - I did not know about "strcmp" - very new to all this c++ stuff!
I guess I can do away with my isValue function! I was proud of it for a short while
Last edited by szlamany; Jul 28th, 2012 at 07:09 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|