Solved

Problem using strstr function

Posted on 2008-10-12
5
630 Views
Last Modified: 2013-12-14
I need to compare 2 strings of different lengths. So I made use of strstr function in which I see whether the string I need is present or not. But using strstr doesnt help because it supports for partial data also which it should not do. So anybody have any idea of how to proceed with?I had actually used strcmp but it doesn work as it compares only one string.

Here is the sample of code I am using:

(0 == strstr(SSOQUALTRec[iSOQTTabCount].sQualAdmTypes,sAdmitType)

where sAdmitType actually has more than 1 string separted by the delimiter. The prb with using strstr here is like this: Say if the AdmitType is given as INPATIENT and then this statement is exec. it works fine. But if the AdmitType is given as INP then also the code works fine which it shouldnt logically.
So my question is there any other string function in which one of the string with only one variable is compared with the set of all strings to see if the first string is present in the second set of strings or not?
Or should I use a while loop to make it work? Or is there any other way to make strstr work to look for complete strings?

0
Comment
Question by:Deepthi_C
  • 4
5 Comments
 
LVL 13

Expert Comment

by:josgood
Comment Utility
This seems like a straightforward way to solve the problem.  The key points are
1)  Iterate over an array of strings, rather than try to use a single string of delimited substrings
2)  Organize the array of strings such that longer strings are tried before shorter strings to get the greediest match possible

#include <windows.h>
#include <iostream>

int IsMatch(char* fullString, char* subString[], int count) {
   for (int i = 0; i < count; i++) {
      if (strstr(fullString,subString[i]) != 0) return i;
   }
   return -1;
}

void ReportMatch(char* fullString, char* subString[], int count) {
   int subScript = IsMatch(fullString, subString, count);
   if (subScript >= 0)
      std::cout << fullString << " contains " << subString[subScript] << std::endl;
   else
      std::cout << "No match found in " << fullString << std::endl;
}

void main() {
//(0 == strstr(SSOQUALTRec[iSOQTTabCount].sQualAdmTypes,sAdmitType)
   // Using an array of allowed matches
   // ..and organizing from longest to shortest
   // ..facilitates while loop
   char* AdmitType[] = { "Inpatient", "Inpat", "INP" };
   int subStringCount = sizeof(AdmitType) / sizeof(char*);
   ReportMatch("The Inpatient123 was Fred",AdmitType,subStringCount);
   ReportMatch("The Inpatie123 was Fred",AdmitType,subStringCount);
   ReportMatch("The INPa123 was Fred",AdmitType,subStringCount);
   ReportMatch("76 trombones in the big parade",AdmitType,subStringCount);
}
0
 

Author Comment

by:Deepthi_C
Comment Utility
Thanks for e reply. But this is not what I am looking for. I think u might have got my question wrong or I might not be clear in my prb.
The prb is that:
I read in 2 strings in which the 1st string contains only 1 string specified by the user. It can be anything.(i meant need not be INPATIENT only). And the other string is a set of strings which can contains more than 1(the strings are seperatd by a comma i.e. ,). Now I am suppose to see whether the 1st string read is present in set of the 2nd string described. I was able to do this using the above set of code  but e prb with strstr function is that it was reading partial admit types also which it should not read. If I use a strcmp function then it can read only 1 string which is also not e solution.
I want a more generalized solution. Hope this could give u an idea of my prb.
Ur solution for my prb will not hold good as I first of all cannot define the Admit Type as it depends on the user. It can be anything except that it is a string.
0
 
LVL 13

Expert Comment

by:josgood
Comment Utility
I think your saying that the user can input two strings such as
"Bob" and
"Bob23,Bob14,Bob,Bob42"
and you to match only the "Bob" section, not the "Bob23", "Bob14", or "Bob42"

Obviously, "Bob" is just an example.  The user can input any string -- where no string contains an embedded comma.

Am I correct?
0
 
LVL 13

Expert Comment

by:josgood
Comment Utility
If that is the case, could you not
ststr("Bob23,Bob14,Bob,Bob42","Bob,")?

Note the comma appended in "Bob,"
0
 
LVL 13

Accepted Solution

by:
josgood earned 500 total points
Comment Utility
I just realized that this may be a homework assignment and so I should refrain from just giving an answer.  I need to guide you rather than tell you.

strstr will serve the purpose for you, if you set up the data appropriately.

You have a known string delimiter -- a comma.  The problem with searching just for "Bob" in my example above is, as you say, it will match Bob42, BobMyUncle, and Bob.

But everything changes (and the answer should be correct) if you search for Bob plus comma, as in "Bob,".

But, you say, what if the set of strings I'm searching ends with "Bob"?

Same idea.  You need to append the delimiter.  If you append the delimiter to both the search string and the set of strings you're searching, then the search will work.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now