Thread: question about strncmp

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    24

    question about strncmp

    I'm using strncmp to return a 1 if the user has typed in recent:

    Code:
    return ((strncmp(checkvalue, "recent", 1) == 0)||(strncmp(checkvalue, "RECENT", 1) == 0)||(strncmp(checkvalue, "Recent", 1) == 0));
    But if the user types in anything that begins in R it still returns 1, is this becuase strncmp is only looking at 1 letter, if so how can I change this?

    Many Thanks

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Use strcmp not strncmp. Strncmp checks only up to a given length; in your case the first character.

    Edit: your code would look cleaner, and work better if you converted the string to upper or lower case first. That way if the user enters "REcent" by accident it would still be valid.
    Last edited by mike_g; 03-22-2008 at 12:13 PM.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    I've just figured out thats what the number 1 was doing in that. How would i use strcmp, just the same but without the number at the end?

    Thanks Again

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yep.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by sweetorangepie View Post
    I've just figured out thats what the number 1 was doing in that. How would i use strcmp, just the same but without the number at the end?

    Thanks Again
    You should learn to use C-reference - it describes exactly what each parametr is, what possible return values are, and how you can investigate the error happend.

    Choose one that you like and start using it when you have a doubt how to call some standard function...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    i have now changed it to:

    Code:
    return ((strcmp(checkvalue, "recent") == 0)||(strcmp(checkvalue, "RECENT") == 0)||(strcmp(checkvalue, "Recent") == 0));
    but it is no longer returning a 1, what am i doing wrong?

    Thanks

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    but it is no longer returning a 1, what am i doing wrong?
    Post the smallest and simplest compilable program that demonstrates the problem. For example, this works for me:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int isRecent(const char *checkvalue);
    
    int main(void)
    {
        const char *str = "Recent";
        printf("&#37;d", isRecent(str));
        return 0;
    }
    
    int isRecent(const char *checkvalue)
    {
        return strcmp(checkvalue, "recent") == 0
            || strcmp(checkvalue, "RECENT") == 0
            || strcmp(checkvalue, "Recent") == 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    This is the simplest version of what i'm trying to achieve:

    Code:
    int main (void)
    
    recent = show_recent(guess);
    				if (0 != recent){
    					printf("\n%s", recent_guess1);
    					printf("%s", recent_guess2);
    					printf("%s", recent_guess3);
    					printf("%s", recent_guess4);
    					break;
    				}
    
    int show_recent( char* checkvar )
    {
    	return ((strcmp(checkvar, "recent") == 0)||(strcmp(checkvar, "RECENT") == 0)||(strcmp(checkvar, "Recent") == 0));
    }
    Can anyone see why this is?

    Thanks

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Post the smallest and simplest compilable program that demonstrates the problem.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM