C++: Comparing Substrings In An Array

Y

Ying Yang

Hi,

What do i use to solve the following problem:

Pseudocode:

if my array of char contains a substring that is equal to string X
{
increment counter;
}
else do not increment counter.


Regards
KLJ
 
Y

Ying Yang

Hi,

What do i use to solve the following problem:

Pseudocode:

if my array of char contains a substring that is equal to string X
{
increment counter;
}
else do not increment counter.

I forgot to add that i would like to do it without using the string data
type if possible.


Regardsd
tyu
 
A

Attila Feher

Ying said:
Hi,

What do i use to solve the following problem:

Pseudocode:

if my array of char contains a substring that is equal to string X
{
increment counter;
}
else do not increment counter.

if I use the strstr function to check if my array of char
contains a substring that is equal to string X
{
++counter;
}
 
T

Thomas Matthews

Ying said:
I forgot to add that i would like to do it without using the string data
type if possible.


Regardsd
tyu

strstr.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
C

Chris Dams

Hello,
I forgot to add that i would like to do it without using the string data
type if possible.

If the string in your array of char is null terminated, you can use

STRSTR(3) Linux Programmer's Manual STRSTR(3)



NAME
strstr - locate a substring

SYNOPSIS
#include <string.h>

char *strstr(const char *haystack, const char *needle);

DESCRIPTION
The strstr() function finds the first occurrence of the
substring needle in the string haystack. The terminating
`\0' characters are not compared.

RETURN VALUE
The strstr() function returns a pointer to the beginning
of the substring, or NULL if the substring is not found.

Bye,
Chris Dams
 
D

Default User

Ying said:
I forgot to add that i would like to do it without using the string data
type if possible.


Why? In C++, you should strive to use std::string whenever possible,
unless there is an overriding reason not to do so. Please explain your
special circumstances. That will help us give an accurate answer.




Brian Rodenborn
 
A

A

Ying said:
Why? In C++, you should strive to use std::string whenever possible,
unless there is an overriding reason not to do so. Please explain your
special circumstances. That will help us give an accurate answer.

Brian Rodenborn

I am learning C++ and since alot of code still contains reminants of C, it's
a good idea to learn how to use functions from the older c library before
moving on to the C++ library.

LKDjdfdfgdfgf
 
G

Gavin Deane

A said:
I am learning C++ and since alot of code still contains reminants of C, it's
a good idea to learn how to use functions from the older c library before
moving on to the C++ library.

"as well as", maybe, but not "before". As Brian said, you should be
using std::string in your own code unless you have a very good reason
not to (other peeple's avoidance of std::string is not a good reason).
You will encounter C-style strings in other people's code, but you
won't be in a position to be maintaining existing code until you've
learnt the languauge. std::string is fundamental to that.

If you want to learn C, learn C. If you want to learn C++, learn C++.
That will involve learning things like C-style strings their library
functions, but that is an advanced C++ topic.

If you want to learn C and C++ at the same time without confusing
yourself to death, you're braver than I am :)

GJD
 
A

A

"as well as", maybe, but not "before". As Brian said, you should be
using std::string in your own code unless you have a very good reason
not to (other peeple's avoidance of std::string is not a good reason).
You will encounter C-style strings in other people's code, but you
won't be in a position to be maintaining existing code until you've
learnt the languauge. std::string is fundamental to that.

Point taken.


Regards
erer
 
M

Mike Wahler

A said:
I am learning C++ and since alot of code still contains reminants of C, it's
a good idea to learn how to use functions from the older c library before
After.



immediately

to the C++ library.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top