strlen()

This is a discussion on strlen() within the C Programming forums, part of the General Programming Boards category; Could you give me a example code to get first 3 letters of a string by using strlen().Thanks. Such as ...

  1. #1
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62

    strlen()

    Could you give me a example code to get first 3 letters of a string by using strlen().Thanks.

    Such as like this, no details just a small example.
    Code:
        
    int strlen ( char * string ) ;
    
    printf ( "%d", strlen( "HelloMum" )) ;
    
    Output is 13
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    do you know what strlen is for? It's for finding the length of a string. If you only need the first 3 characters, you don't need strlen.

  3. #3
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    thanks, do you know what could I use to get first 3 characters of a string?
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835

  5. #5
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    thanks dude. This website has a good example who wants to learn about it, should check it: Link
    Last edited by BoneXXX; 06-17-2007 at 12:08 AM.
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    This website also has good examples, read the FAQ :\

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I looked at the behavior of strncpy() and if the string it's copying is longer than the number of bytes you tell it to copy, it doesn't null-terminate the result. So you need to define a buffer array of 4 chars, and set the last element to '\0' before using strncpy().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing around strlen :)
    By audinue in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 04:22 PM
  2. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 12:15 PM
  3. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 11:28 AM
  4. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  5. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21