Checking if a string is null

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veenasheela
    New Member
    • Jul 2007
    • 1

    Checking if a string is null

    A string can be checked if it is empty using the following code

    if (strlen (string) = 0)

    But, instead of using strlen, how to determine if a string is empty using the first character of the string
  • kky2k
    New Member
    • May 2007
    • 34

    #2
    Originally posted by veenasheela
    A string can be checked if it is empty using the following code

    if (strlen (string) = 0)

    But, instead of using strlen, how to determine if a string is empty using the first character of the string
    Though u didn't posted it inside CODE tags i pointed out a common mistake...

    instead of ...
    Code:
    if(strlen(string) == 0)
    u was mentioned its as..
    Code:
     if(stlen(string) = 0)
    And regarding ur question..Accor ding to me its not possible using the first character

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      Originally posted by kky2k
      Though u didn't posted it inside CODE tags i pointed out a common mistake...

      instead of ...
      Code:
      if(strlen(string) == 0)
      u was mentioned its as..
      Code:
       if(stlen(string) = 0)
      And regarding ur question..Accor ding to me its not possible using the first character
      If it's a C string it is, the first char will be the null terminator '\0'.

      If it's not a C string, it's dependent on the programmer to have initialized it as "null" (I think - I'm not sure of any other way to directly check this).

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by veenasheela
        But, instead of using strlen, how to determine if a string is empty using the first character of the string
        The C string is empty if the first byte is equal to '\0', a null terminator. This does not work for the C++ string class.

        Comment

        Working...