Problem using strstr function

Posted on 2008-10-12
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

615 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