counting within a string

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

    #1

    counting within a string

    string s =
    "2631:3000:3292 :3610:3800:4333 :5294:5698:6000 :6398x3:7906:15 000:16666x2:186 15:20000"

    I need to split it, make ints and get the median of the numbers.

    Ideal first step is to count instances of ":"

    for (a = 0; a != s.length(); ++a)
    {
    if (s[i] = ":") cout << a << endl;
    }

    Error: error C2440: '=' : cannot convert from 'const char [2]' to
    'char

    So my questions are - is this the best way to count elements on a
    string and what is the correct incantation for s[i] where i refers to
    the position int he string s?

  • Victor Bazarov

    #2
    Re: counting within a string

    pkirk25 wrote:
    string s =
    "2631:3000:3292 :3610:3800:4333 :5294:5698:6000 :6398x3:7906:15 000:16666x2:186 15:20000"
    >
    I need to split it, make ints and get the median of the numbers.
    >
    Ideal first step is to count instances of ":"
    >
    for (a = 0; a != s.length(); ++a)
    {
    if (s[i] = ":") cout << a << endl;
    }
    >
    Error: error C2440: '=' : cannot convert from 'const char [2]' to
    'char
    >
    So my questions are - is this the best way to count elements on a
    string and what is the correct incantation for s[i] where i refers to
    the position int he string s?
    You might get your stuff working if you use 'istringstream' and input from
    it in a loop: a number, then a char, then again a number, and so on until
    the
    end of the stream.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • pkirk25

      #3
      Re: counting within a string

      I'm new to C++. Do you mind showing a simple example , say finding the
      letter "a" in "cat" using cat as an STL strign as your start point? I
      can't work out how to get form s[i] to the new tyoe of varibale you are
      referring to.

      Comment

      • Peter Jansson

        #4
        Re: counting within a string

        pkirk25 wrote:
        I'm new to C++. Do you mind showing a simple example , say finding the
        letter "a" in "cat" using cat as an STL strign as your start point? I
        can't work out how to get form s[i] to the new tyoe of varibale you are
        referring to.
        >
        (Personal view: Please quote somewthing of the orginal message when
        replying, it is much easier to understand you.)

        A simple example program follows:

        #include <iostream>
        #include <sstream>
        #include <string>
        int main()
        {
        // Example of construction of an istringstream:
        std::istringstr eam iss(std::string ("cat"));
        // Example on how to read from the istringstream:
        char ch;
        while(iss>>ch)
        {
        std::cout<<"Fou nd a char: '"<<ch<<"'."
        <<(ch=='a'?" Hey, it was an 'a'!":"")<<'\n' ;
        }
        }


        This outputs on my computer:

        Found a char: 'c'.
        Found a char: 'a'. Hey, it was an 'a'!
        Found a char: 't'.


        Sincerely,

        Peter Jansson


        Comment

        • Michael

          #5
          Re: counting within a string

          Ideal first step is to count instances of ":"
          >
          for (a = 0; a != s.length(); ++a)
          {
          if (s[i] = ":") cout << a << endl;
          }
          >
          Error: error C2440: '=' : cannot convert from 'const char [2]' to
          'char
          >
          So my questions are - is this the best way to count elements on a
          string and what is the correct incantation for s[i] where i refers to
          the position int he string s?
          To answer your original question:
          for (unsigned int a = 0; a != s.length(); ++a)
          {
          if (s[a] == ':') cout << a << endl;
          }

          Notes: == instead of = (this caused your compiler error), single quotes
          instead of double quotes, and a instead of i.

          To answer the subsequent question, here's the piece of code I use to
          split strings. Use it or ignore it as you will:
          vector<stringsp lit(const string& s, char delimiter)
          {
          vector<stringre sults;

          string::size_ty pe startIndex = 0;
          while (true) {
          string::size_ty pe pos = s.find(delimite r, startIndex);
          results.push_ba ck(s.substr(sta rtIndex, pos - startIndex));
          if (pos == string::npos) {
          break;
          }
          startIndex = pos + 1;
          }
          return results;
          }

          You would call split(s, ':')

          Michael

          Comment

          • pkirk25

            #6
            Thanks

            Michael you make it look sickeningly easy!

            I will take your code, make results a vector of ints and use c_str to
            populate it.

            Many thanks all.

            Comment

            • Default User

              #7
              Re: counting within a string

              pkirk25 wrote:
              string s =
              "2631:3000:3292 :3610:3800:4333 :5294:5698:6000 :6398x3:7906:15 000:16666x
              2:18615:20000"
              >
              I need to split it, make ints and get the median of the numbers.
              >
              Ideal first step is to count instances of ":"
              >
              for (a = 0; a != s.length(); ++a)
              {
              if (s[i] = ":") cout << a << endl;
              }
              >
              Error: error C2440: '=' : cannot convert from 'const char [2]' to
              'char
              You are comparing the string literal ":" to a character. The correct
              character comparison would be

              if (s[i] = ':')

              So my questions are - is this the best way to count elements on a
              string
              That's one way. You can also use successive calls to s.find().

              and what is the correct incantation for s[i] where i refers to
              the position int he string s?
              What does that mean?




              Brian

              Comment

              • pkirk25

                #8
                Re: counting within a string

                Sadly, it means it was bedtime and making sure teeth were brushed and
                stories read was more important than spell checking.

                Comment

                • BobR

                  #9
                  Re: counting within a string


                  Default User wrote in message <4ol9q7Ff69fbU1 @individual.net >...
                  >pkirk25 wrote:
                  >
                  >string s =
                  >"2631:3000:329 2:3610:3800:433 3:5294:5698:600 0:6398x3:7906:1 5000:16666x
                  >2:18615:2000 0"
                  >>[snip]
                  >
                  >You are comparing the string literal ":" to a character. The correct
                  >character comparison would be
                  >
                  if (s[i] = ':')
                  OUCH!! A comparison would be:

                  if( s[i] == ':' ){ /* do something */ }

                  <G>
                  --
                  Bob R
                  POVrookie


                  Comment

                  Working...