Thread: How to check if a string has at least one character.

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    33

    How to check if a string has at least one character.

    For example: If you want to check whether a string has a '@' in it. If not, then ask the user to input another string.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You can make a function which loops through a string until '@' is found.

    Don't forget to have a maximum string length entered into the function
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Good call, Tclausex

    I'd recommend strnchr though
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    @Click_here: Why?

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I don't think I do any more, because I've just discovered that it is a non-standard function!

    However, the reason I did recommend it was because if no NUL character on the end of the string (for whatever reason) and the character is not found, the function will not return a pointer to non-valid memory.

    strchr will keep looking until if finds a match -> It will keep going outside the string.

    Here is an example that although is not very realistic, and I'd imagine that it is more of an issue with non-hosted environments where variables are packed together.
    Code:
    /*
    Note that the error has been inserted intentionally in a unrealistic situation.
    */
    int main(void)
    {
        const char s[5] = "abcde";
        const char t[] = "fghij";
        char *p;
    
    
        p = strchr(s, 'f');
    
    
        if (p)
        {
            puts(p);
        }
    
    
        return 0;
    }
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    memchr is better for that, since strnchr will waste cycles checking for the null character on every iteration. I think it's only necessary in the unusual situation where you want to search, for example, four characters of a string without knowing the length of the string in advance. That way it won't go past the null character if the string has less than four characters, and it won't search past the fourth character if the string has more than four characters.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I think that the real danger is strchr returning a pointer to a location which is not in the buffer because the NUL character is missing.

    memchr is probably the way to go.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check to check string for a series of characters
    By robi in forum C Programming
    Replies: 1
    Last Post: 02-28-2012, 05:42 PM
  2. Replies: 3
    Last Post: 02-26-2009, 12:24 PM
  3. Help! with character validity check
    By Tdankert in forum C Programming
    Replies: 19
    Last Post: 07-30-2007, 08:41 PM
  4. Check if next character is a letter?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2002, 06:02 PM
  5. Replies: 12
    Last Post: 01-12-2002, 09:57 AM