Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Problem using strstr function

Posted on 2008-10-12
Medium Priority
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?

Question by:Deepthi_C
  • 4
LVL 13

Expert Comment

ID: 22697300
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;
      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);

Author Comment

ID: 22697566
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.
LVL 13

Expert Comment

ID: 22697845
I think your saying that the user can input two strings such as
"Bob" and
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?
LVL 13

Expert Comment

ID: 22697856
If that is the case, could you not

Note the comma appended in "Bob,"
LVL 13

Accepted Solution

josgood earned 1500 total points
ID: 22697928
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.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Suggested Courses

580 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