Solved

String compare

Posted on 2003-12-07
11
325 Views
Last Modified: 2010-04-01
String A =  "there is a UDP"
String B = "UDP"

How to find out whether string A contains "UDP"?
should i use string compare or anything?
should i str compare String A and B??
Could anyone advise

Thanks
0
Comment
Question by:OwenMania
[X]
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
11 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 9892051
man strstr

0
 

Accepted Solution

by:
Drew_Benton earned 50 total points
ID: 9892110
grg99 is right what function but ill explain:

void TestFunction()
{
char* A = "there is a UDP";       // Your first orig string
char* B = "UDP";                      // Your second string you're looking for

char* result = strstr(A,B);         // strstr returns a pointer of char* of the first whole occurance

if(result)                // if the pointer actually has a location now ie: the WHOLE string was found
   ;                        // String was found, so do what ever you want now
else
   ;                        // String was not found, so dont do what was planned
}

- Drew
0
 
LVL 1

Expert Comment

by:igor_sk
ID: 9892499
All you guys said is correct in C.
But, if you use C++ (I suppose you do) and you use 'string' class to store your strings. Then you should use find() method of the class string:

string A =  "there is a UDP";
string B = "UDP";

if (A.find(B) != -1)
     cout << "String A contains string B" << endl;
else
     cout << "String B was not found in string A" << endl;
0
Independent Software Vendors: 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!

 

Expert Comment

by:Drew_Benton
ID: 9892812
hey ques: i was thinking the same thing as igor_sk siad, but it was part of a CString class (MFC), now this string class, umm honeslty, nvr heard of it b4 in C++, so what do i need to do to use it or is it a default type?
0
 

Author Comment

by:OwenMania
ID: 9894003
But how come i cannot declare String??
There's an error: Undeclared identifier "string"

i have already included string.h

#include <iostream>
#include <fstream.h>
#include <stdlib.h>
#include <STRING>

int main () {

String A;
return 0;
}
0
 

Expert Comment

by:Drew_Benton
ID: 9894065
try:
#include <STRING.H>
then that code
0
 

Expert Comment

by:Drew_Benton
ID: 9894068
then String/string
0
 

Author Comment

by:OwenMania
ID: 9894078
i have tried but failed
0
 

Author Comment

by:OwenMania
ID: 9894151
The method is:
  #include <string.h>
  using namespace std;

Done
0
 

Expert Comment

by:Drew_Benton
ID: 9894164
Ahh here we go, take a look at this:

http://www.bgsu.edu/departments/compsci/docs/string.html
0
 

Expert Comment

by:Drew_Benton
ID: 9894177
If that doesnt work, id recommend taking a look at my 1st post example of using the char*'s and try that to do what you want to do
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

732 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