Thread: Take out chars in string

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22

    Smile Take out chars in string

    Hello World!

    If now i want to take the 3 first chars in a string, to compare them to something else.
    How do i easiest only get those 3 chars of the string when i want to compare?

  2. #2
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Code:
        const char* s1 = "Hello world";
    
        char s2[4];
    
        //3 - You want 3 characters
        //1 - The NULL char
        //4 = 3 + 1
    
        strcpy(s2, s1);
    
        if(strcmp(s2, "Hel"))
            printf("OK");

  3. #3
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    Quote Originally Posted by audinue View Post
    Code:
        const char* s1 = "Hello world";
    
        char s2[4];
    
        //3 - You want 3 characters
        //1 - The NULL char
        //4 = 3 + 1
    
        strcpy(s2, s1);
    
        if(strcmp(s2, "Hel"))
            printf("OK");
    Dont i need to add the NULL?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, it looks like audinue's example is incorrect. Using strcpy() in that way will result in a buffer overrun. You should use strncpy() instead, e.g.,
    Code:
    const char* s1 = "Hello world";
    
    char s2[4];
    
    //3 - You want 3 characters
    //1 - The NULL char
    //4 = 3 + 1
    strncpy(s2, s1, 3);
    s2[3] = '\0';
    
    if(strcmp(s2, "Hel"))
        printf("OK");
    EDIT:
    Then again, there is strncmp(), so life is easier than that:
    Code:
    const char* s1 = "Hello world";
    
    if (strncmp(s1, "Hel", 3) == 0)
    {
        printf("There's a match!\n");
    }
    Last edited by laserlight; 07-05-2008 at 03:50 PM.
    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

  5. #5
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Sorry, revision:

    Code:
    char *s1 = "1234567";
    
    char *s2 = malloc(4 * sizeof(char));
    
    memcpy(s2, s1, 3);
    s2[3] = '\0';
    
    if(strcmp(s2, "123") == 0)
    {
        printf("'%s' is identical to '123'.", s2);
    }
    You're right. We must add the NULL char in the end of the string.

    strcpy function

    Purpose:
    Copies a string.

    Syntax:
    char * strcpy(char * restrict targetstring, const char * restrict sourcestring);

    Declared in:
    <string.h> (strcpy)

    The function copies sourcestring, including the terminating null character ('\0'), to targetstring. The behavior is undefined if the two strings overlap.

    Returns:
    The argument targetstring.

    Conclusion:
    This function copies the whole string.
    Last edited by audinue; 07-05-2008 at 03:47 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, revision:
    Except that now you changed s1 from const char* to char* when you should have left it alone

    Also, note that sizeof(char) is always 1. Anyway, I think that strncmp() works best here.
    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

  7. #7
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Wondeful, thanks laserlight!

    Btw is there function like strrncmp
    Compare the right-part of the string?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Btw is there function like strrncmp
    Compare the right-part of the string?
    Not in the standard library, but it is not too difficult to write one.
    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

  9. #9
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    What happens without the NULL.
    Will it pickup data from memory until next NULL?

    And when i do the:
    Code:
    str2[3] = "\0";
    Get: warning: assignment makes integer from pointer without a cast


    But when i instead off "\0", is using ascii:

    Code:
    str2[3] = 0;
    its working great.
    Why?
    Last edited by MKirstensen; 07-05-2008 at 04:55 PM.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because double quotes is a way of writing short-hand arrays, ie
    Code:
    char test[6] = "hello";
    is the same as

    Code:
    char test[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
    Notice it's nul terminated, thus
    Code:
    str[3] = "\0";
    is really
    Code:
    str[3] = {'\0', '\0'};
    Which of course won't work as, str2[3] is a single character. And it's also a string literal (that is stored in read-only memory somewhere thus the pointer cast error). So you probably want
    Code:
    str2[3] = '\0';
    Where single quotes are represent an ASCII constant (evaluated to a number anyway, in this case 0 -- look up an ASCII table).

    As per the other thread, you really need to get yourself a book.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    Quote Originally Posted by zacs7 View Post
    As per the other thread, you really need to get yourself a book.
    In know, but cant find a damn thing on this particular subject.... im sorry.
    Still bee nice if someone would answer me on what the consequences for a string without NULL is...

    To me its looks like it includes some of the other data.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I believe the behavior is undefined. On many or most systems, it depends upon memory access restrictions put in place by the OS, compiler, etc. etc..

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And when you're talking about the nul terminator it's nul or NUL not NULL

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    And when you're talking about the nul terminator it's nul or NUL not NULL
    No, it is called a "null character", according to C99, and consequently it can be called a "null terminator". Checking my English dictionary, I can tell you that "nul" is not a word. If anything, it is an abbreviation for "null". Of course, it is bad to call it a "NULL terminator" since that may be confused with the macro NULL, though '\0' and NULL do have the same integral value.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM