Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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: …
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

670 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