C++: Comparing Substrings In An Array

Discussion in 'C++' started by Ying Yang, Sep 24, 2003.

  1. Ying Yang

    Ying Yang Guest

    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
    Ying Yang, Sep 24, 2003
    #1
    1. Advertising

  2. Ying Yang

    Ying Yang Guest

    Re: 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.


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


    Regardsd
    tyu
    Ying Yang, Sep 24, 2003
    #2
    1. Advertising

  3. Ying Yang

    Attila Feher Guest

    Re: Comparing Substrings In An Array

    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
    Attila Feher, Sep 24, 2003
    #3
  4. Re: Comparing Substrings In An Array

    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
    Thomas Matthews, Sep 24, 2003
    #4
  5. Ying Yang

    Chris Dams Guest

    Re: Comparing Substrings In An Array

    "Ying Yang" <> 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
    Chris Dams, Sep 24, 2003
    #5
  6. Ying Yang

    Default User Guest

    Re: Comparing Substrings In An Array

    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
    Default User, Sep 24, 2003
    #6
  7. Ying Yang

    A Guest

    Re: Comparing Substrings In An Array

    > 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
    A, Sep 25, 2003
    #7
  8. Ying Yang

    Gavin Deane Guest

    Re: Comparing Substrings In An Array

    "A" <> wrote in message news:<>...
    > > 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
    Gavin Deane, Sep 25, 2003
    #8
  9. Ying Yang

    A Guest

    Re: Comparing Substrings In An Array


    > > > 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
    A, Sep 26, 2003
    #9
  10. Ying Yang

    Mike Wahler Guest

    Re: Comparing Substrings In An Array

    "A" <> wrote in message
    news:...
    > > 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
    Mike Wahler, Sep 27, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leandro Pardini

    Binary files, substrings and (un)packing.

    Leandro Pardini, Oct 25, 2003, in forum: Perl
    Replies:
    1
    Views:
    581
    Jim Gibson
    Oct 27, 2003
  2. Markus Dehmann

    regex: How to extract substrings?

    Markus Dehmann, Dec 10, 2005, in forum: Java
    Replies:
    2
    Views:
    787
    IchBin
    Dec 10, 2005
  3. mrstephengross

    Compile typedef-ed substrings

    mrstephengross, Apr 4, 2005, in forum: C++
    Replies:
    7
    Views:
    390
  4. Sam Larbi
    Replies:
    10
    Views:
    277
    Sam Larbi
    Nov 28, 2007
  5. Amittai Aviram
    Replies:
    0
    Views:
    92
    Amittai Aviram
    Nov 15, 2004
Loading...

Share This Page