473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C++: Comparing Substrings In An Array

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


Jul 19 '05 #1
9 5281

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
Jul 19 '05 #2
Ying Yang wrote:
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;
}
--
Attila aka WW
Jul 19 '05 #3
Ying Yang wrote:
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


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

Jul 19 '05 #4
"Ying Yang" <Yi******@hotmail.com> writes:

Hello,
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.


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
Jul 19 '05 #5
Ying Yang wrote:
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
Jul 19 '05 #6
A
> Ying Yang wrote:
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


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
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.518 / Virus Database: 316 - Release Date: 11/09/2003
Jul 19 '05 #7
"A" <A@iprimus.com.au> wrote in message news:<3f********@news.iprimus.com.au>...
Ying Yang wrote:
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


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
Jul 19 '05 #8
A
Ying Yang wrote:

> 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


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.


Point taken.
Regards
erer


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.518 / Virus Database: 316 - Release Date: 11/09/2003
Jul 19 '05 #9
"A" <A@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...
Ying Yang wrote:
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


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.
moving on
immediately
to the C++ library.


-Mike
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Leandro Pardini | last post by:
Hello there, I'm trying to process a binary file and I really don't know how. The story: gPhoto2 downloads the images from my camera just fine, but small areas of 10x3 pixels are screwed up. I...
11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
9
by: C3 | last post by:
I have to process some data in C that is given to me as a char * array. I have a fairly large number of substrings (well, they're not actually printable, but let's treat them as strings) that I...
10
by: CodeRazor | last post by:
Is there a string method that allows gives you the number of times a substring appears in your string. Looping through my string performing IndexOf("substring",startPos), seems like overkill. ...
2
by: Jason Barnett | last post by:
I thought I had seen a method for returning the number of occurances of a substring within a string object, but I can't seem to find it now. Does anyone know of something? I know I can parse...
8
by: girish | last post by:
Hi, I want to generate all non-empty substrings of a string of length >=2. Also, each substring is to be paired with 'string - substring' part and vice versa. Thus, gives me , , , , , ] etc....
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
1
by: ngmr80 | last post by:
Hi, I'm experiencing a problem when trying to capture substrings with preg_match_all() from strings like "set('Hello','World')" using the following Regular Expression (PERL syntax): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.