Solved

String compare

Posted on 2003-12-07
11
297 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
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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now