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. Testing if a string is empty

Testing if a string is empty

Scheduled Pinned Locked Moved C / C++ / MFC
testingbeta-testingtutorialquestion
8 Posts 6 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.
  • P Offline
    P Offline
    Programm3r
    wrote on last edited by
    #1

    Hi all, Simple question .... (funny that I can't remember how to do it) I have variable: char TestString[3]; How can one test if the TestString is Empty :confused: I have tried the following, without success: if (TestString == "") { .... } Thanx in advance

    The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

    P J C C P 5 Replies Last reply
    0
    • P Programm3r

      Hi all, Simple question .... (funny that I can't remember how to do it) I have variable: char TestString[3]; How can one test if the TestString is Empty :confused: I have tried the following, without success: if (TestString == "") { .... } Thanx in advance

      The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      strlen.

      Programm3r wrote:

      char TestString[3];

      In this case, it will not be an empty string.Contains some garbage. try,

      char TestString[3] = {0};
      or
      char TestString[3] = "";
      cout<

      It will give you desired output. i.e. output will be 0, which indiacates empty string.

      Prasad
      Notifier using ATL | Operator new[],delete[][^]

      1 Reply Last reply
      0
      • P Programm3r

        Hi all, Simple question .... (funny that I can't remember how to do it) I have variable: char TestString[3]; How can one test if the TestString is Empty :confused: I have tried the following, without success: if (TestString == "") { .... } Thanx in advance

        The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

        J Offline
        J Offline
        joelgarabedian
        wrote on last edited by
        #3

        You need to do if(strlen(TestString) == 0) or if(strcmp(TestString, "") == 0) Remember that your string will not be initialised when you declare it though, so neither test is valid until you've put something into the string (NULL for the first character at the very least). Or you can do the following... memset(&TestString[0], 0, sizeof(TestString)); Hope this helps, Joel.

        1 Reply Last reply
        0
        • P Programm3r

          Hi all, Simple question .... (funny that I can't remember how to do it) I have variable: char TestString[3]; How can one test if the TestString is Empty :confused: I have tried the following, without success: if (TestString == "") { .... } Thanx in advance

          The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Your string

          Programm3r wrote:

          char TestString[3];

          ``is not empty, it's random (junk) initialised (end it is not `NULL` terminated!). maybe the following code snipped will help you: char TestString[3]=""; if (!strcmp(TestString, "")) { printf("empty\n"); } else { printf("not empty: %s\n", TestString); } If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile. ``

          1 Reply Last reply
          0
          • P Programm3r

            Hi all, Simple question .... (funny that I can't remember how to do it) I have variable: char TestString[3]; How can one test if the TestString is Empty :confused: I have tried the following, without success: if (TestString == "") { .... } Thanx in advance

            The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            if (!TestString[0]) { // The first character is null, therefore it's empty } That's assuming you have set it to be empty. This is one of many reasons to avoid char arrays and use std::string or CString. Unless you're trapped in C, which I seem to recall, you are. In this case, make sure you use memset to clear the contents of a new char array, so the above test will work. All the other tests will do the same thing, they will look to see how many characters there are before a NULL.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            1 Reply Last reply
            0
            • P Programm3r

              Hi all, Simple question .... (funny that I can't remember how to do it) I have variable: char TestString[3]; How can one test if the TestString is Empty :confused: I have tried the following, without success: if (TestString == "") { .... } Thanx in advance

              The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

              P Offline
              P Offline
              Programm3r
              wrote on last edited by
              #6

              :):-D:laugh: Regards

              The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

              K 1 Reply Last reply
              0
              • P Programm3r

                :):-D:laugh: Regards

                The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

                K Offline
                K Offline
                kakan
                wrote on last edited by
                #7

                In addition to all the things said, it's always good programming practice to initialize the variables before testing on them. IMO, one common reason for problem is that memory is allocated differently in debug and release mode. In debug, the allocated buffer becomes filled with null-bytes. In release-build, it isn't. It's filled with junk characters (whatever is on the stack). So (without initializing) testing for '\0' at index 0 works in debug mode, but not in release mode. That's something to watch out for..

                Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                C 1 Reply Last reply
                0
                • K kakan

                  In addition to all the things said, it's always good programming practice to initialize the variables before testing on them. IMO, one common reason for problem is that memory is allocated differently in debug and release mode. In debug, the allocated buffer becomes filled with null-bytes. In release-build, it isn't. It's filled with junk characters (whatever is on the stack). So (without initializing) testing for '\0' at index 0 works in debug mode, but not in release mode. That's something to watch out for..

                  Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  kakan wrote:

                  n debug, the allocated buffer becomes filled with null-bytes. In release-build, it isn't. It's filled with junk characters (whatever is on the stack).

                  and this is the source of very weird bugs. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                  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