Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Basic C++ String Question

Basic C++ String Question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

    hello guys... I have this piece of code. Lets say my name contains 'u' one time, my code will reduce the string size by 1, exludes the 'u' from string and displays it on screen.

    if (chr[i] == 'u'){
    count++;
    }
    else{
    cout<<chr[i];
    }
    cout<<"\nIt has "<<strlen(chr) - count<<" characters.";

    Now I am using if - else here...can these two tasks be done in just if ? Like incrementing count and displaying name in if without using else.

    G S 2 Replies Last reply
    0
    • A AmbiguousName

      hello guys... I have this piece of code. Lets say my name contains 'u' one time, my code will reduce the string size by 1, exludes the 'u' from string and displays it on screen.

      if (chr[i] == 'u'){
      count++;
      }
      else{
      cout<<chr[i];
      }
      cout<<"\nIt has "<<strlen(chr) - count<<" characters.";

      Now I am using if - else here...can these two tasks be done in just if ? Like incrementing count and displaying name in if without using else.

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      Yes:

      if(chr[i] != 'u' || !++count) {
      cout << chr[i];
      }

      Whether you should do this or not is another question - it's a lot harder to understand.

      S 1 Reply Last reply
      0
      • G Graham Breach

        Yes:

        if(chr[i] != 'u' || !++count) {
        cout << chr[i];
        }

        Whether you should do this or not is another question - it's a lot harder to understand.

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #3

        Ummmm, definitely not advisable: 1. Whether this works or not is compiler dependend - a compiler is free to evaluate the left or right side of an || operator in whatever order it likes! 2. Like you said, hardly anyone will understand, and even those that do will doubt what it does was actially intended, and might decide to 'fix' it wrongly. Apart from that - interesting thought; I was about to answer 'no' ;)

        C 1 Reply Last reply
        0
        • A AmbiguousName

          hello guys... I have this piece of code. Lets say my name contains 'u' one time, my code will reduce the string size by 1, exludes the 'u' from string and displays it on screen.

          if (chr[i] == 'u'){
          count++;
          }
          else{
          cout<<chr[i];
          }
          cout<<"\nIt has "<<strlen(chr) - count<<" characters.";

          Now I am using if - else here...can these two tasks be done in just if ? Like incrementing count and displaying name in if without using else.

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          I think there's some piece of information missing: Your code doesn't show a loop, or any other construct that loops through the characters in chr, and there's no point in displaying count before the end of that loop. Then in your request you suggest displaying 'name' within your if block, but there's no variable name, and if this should refer to chr, there is no point in displaying it fully for every value of i that you use in that if statement. Unless your string chr will always be exactly "u" (i. e. a string of 1 character, which is 'u'), your question, taken literally, doesn't make much sense. Besides, why do you want to get rid of that else statement?

          1 Reply Last reply
          0
          • S Stefan_Lang

            Ummmm, definitely not advisable: 1. Whether this works or not is compiler dependend - a compiler is free to evaluate the left or right side of an || operator in whatever order it likes! 2. Like you said, hardly anyone will understand, and even those that do will doubt what it does was actially intended, and might decide to 'fix' it wrongly. Apart from that - interesting thought; I was about to answer 'no' ;)

            C Offline
            C Offline
            Craig Longman
            wrote on last edited by
            #5

            Stefan63 wrote:

            1. Whether this works or not is compiler dependend - a compiler is free to evaluate the left or right side of an || operator in whatever order it likes!

            Erm... hold on a sec here... Short-circuiting IS mandated, and can only work if it's evaluated in order. It's always true for PODs. The only caveat is, if it is a class, and that class has an overloaded && or || operator. Then, both sides will be evaluated, so that the compiler can pass the element on the right hand side as a parameter. One (very) good reason to never, ever overload them. At least, this is the way I understand it, and I can't imagine anything undoing that, as tons of code that did this:

            if( pszString != NULL && *pszString != '\0' )

            would randomly crash if *pszString was evaluated first. Course, in this example the actual comparisons can be removed, but I was trying to be explicit. N'est pas? -- CraigL

            S 1 Reply Last reply
            0
            • C Craig Longman

              Stefan63 wrote:

              1. Whether this works or not is compiler dependend - a compiler is free to evaluate the left or right side of an || operator in whatever order it likes!

              Erm... hold on a sec here... Short-circuiting IS mandated, and can only work if it's evaluated in order. It's always true for PODs. The only caveat is, if it is a class, and that class has an overloaded && or || operator. Then, both sides will be evaluated, so that the compiler can pass the element on the right hand side as a parameter. One (very) good reason to never, ever overload them. At least, this is the way I understand it, and I can't imagine anything undoing that, as tons of code that did this:

              if( pszString != NULL && *pszString != '\0' )

              would randomly crash if *pszString was evaluated first. Course, in this example the actual comparisons can be removed, but I was trying to be explicit. N'est pas? -- CraigL

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              I've learned not to rely on evaluation order, but I just looked it up and found my argument would only be true in cases where both argument sneed to be evaluated in any case, such as in an addition. In fact, Stroustrup explicitely names the exception, and that exception is exactly what you said: "The operators ' (comma), && (logical and), and || (logical or) guarantee that their left-hand operand is evaluated before their right-hand operand." (Bjarne Stroustrup, The C++ Programming Language, Third edition, page 123) So I stand corrected. Thanks for pointing this out.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups