[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

String compare

Posted on 2003-12-07
11
Medium Priority
?
339 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

656 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