Link to home
Start Free TrialLog in
Avatar of Deepthi_C
Deepthi_CFlag for India

asked on

Problem using strstr function

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?

Avatar of josgood
josgood
Flag of United States of America image

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);
}
Avatar of Deepthi_C

ASKER

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.
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?
If that is the case, could you not
ststr("Bob23,Bob14,Bob,Bob42","Bob,")?

Note the comma appended in "Bob,"
ASKER CERTIFIED SOLUTION
Avatar of josgood
josgood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial