C++: Comparing Substrings In An Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ying Yang

    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




  • Ying Yang

    #2
    Re: Comparing Substrings In An Array


    [color=blue]
    > 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.[/color]

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


    Regardsd
    tyu


    Comment

    • Attila Feher

      #3
      Re: Comparing Substrings In An Array

      Ying Yang wrote:[color=blue]
      > 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.[/color]

      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


      Comment

      • Thomas Matthews

        #4
        Re: Comparing Substrings In An Array

        Ying Yang wrote:[color=blue][color=green]
        >>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.[/color]
        >
        >
        > I forgot to add that i would like to do it without using the string data
        > type if possible.
        >
        >
        > Regardsd
        > tyu
        >
        >[/color]

        strstr.


        --
        Thomas Matthews

        C++ newsgroup welcome message:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book
        http://www.sgi.com/tech/stl -- Standard Template Library

        Comment

        • Chris Dams

          #5
          Re: Comparing Substrings In An Array

          "Ying Yang" <YingYang@hotma il.com> writes:

          Hello,
          [color=blue][color=green]
          >> 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.[/color][/color]
          [color=blue]
          >I forgot to add that i would like to do it without using the string data
          >type if possible.[/color]

          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

          Comment

          • Default User

            #6
            Re: Comparing Substrings In An Array

            Ying Yang wrote:
            [color=blue]
            > I forgot to add that i would like to do it without using the string data
            > type if possible.[/color]


            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

            Comment

            • A

              #7
              Re: Comparing Substrings In An Array

              > Ying Yang wrote:[color=blue]
              >[color=green]
              > > I forgot to add that i would like to do it without using the string data
              > > type if possible.[/color]
              >
              >
              > 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[/color]

              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


              Comment

              • Gavin Deane

                #8
                Re: Comparing Substrings In An Array

                "A" <A@iprimus.com. au> wrote in message news:<3f728ade_ 1@news.iprimus. com.au>...[color=blue][color=green]
                > > Ying Yang wrote:
                > >[color=darkred]
                > > > I forgot to add that i would like to do it without using the string data
                > > > type if possible.[/color]
                > >
                > >
                > > 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[/color]
                >
                > 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.[/color]

                "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

                Comment

                • A

                  #9
                  Re: Comparing Substrings In An Array

                  [color=blue][color=green][color=darkred]
                  > > > Ying Yang wrote:
                  > > >
                  > > > > I forgot to add that i would like to do it without using the string[/color][/color][/color]
                  data[color=blue][color=green][color=darkred]
                  > > > > 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[/color]
                  > >
                  > > I am learning C++ and since alot of code still contains reminants of C,[/color][/color]
                  it's[color=blue][color=green]
                  > > a good idea to learn how to use functions from the older c library[/color][/color]
                  before[color=blue][color=green]
                  > > moving on to the C++ library.[/color]
                  >
                  > "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.[/color]

                  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


                  Comment

                  • Mike Wahler

                    #10
                    Re: Comparing Substrings In An Array

                    "A" <A@iprimus.com. au> wrote in message
                    news:3f728ade_1 @news.iprimus.c om.au...[color=blue][color=green]
                    > > Ying Yang wrote:
                    > >[color=darkred]
                    > > > I forgot to add that i would like to do it without using the string[/color][/color][/color]
                    data[color=blue][color=green][color=darkred]
                    > > > type if possible.[/color]
                    > >
                    > >
                    > > 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[/color]
                    >
                    > I am learning C++ and since alot of code still contains reminants of C,[/color]
                    it's[color=blue]
                    > a good idea to learn how to use functions from the older c library before[/color]

                    After.
                    [color=blue]
                    > moving[/color]
                    [color=blue]
                    >on[/color]

                    immediately
                    [color=blue]
                    > to the C++ library.[/color]

                    -Mike


                    Comment

                    Working...