?
Solved

String compare

Posted on 2003-12-07
11
Medium Priority
?
334 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 200 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
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!

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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 This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.
Suggested Courses

762 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