Thread: Taking only first 100 chars of a string

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    19

    Taking only first 100 chars of a string

    Is there a function that will, if a string has more that 100 chars in it, just cut off anything after the 100th char?

    If not, any hints on how to start writing this function myself?

    Thanks

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Once you understand the basics of working with char arrays, this shouldn't be hard to implement. Starting at the pointer to the beginning of the string, step through it incrementing the pointer and a counter for length. If you get to a null character, that's the end of the string, and you have it's length. If the counter reaches 100 first, insert a null character there, cutting your string to length 100.

    Try making some code to do the task, using what I've written if it helps you. As a note, there are library functions to do this, and no, it's not a good idea to reinvent the wheel in general, but if you are to be comfortable with C's representation of strings, you should be able to write this from scratch.
    hello, internet!

  3. #3
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    create new array which is as long as you want and simply copy these characters from
    array to other.

    strncpy () helps you copy characters from array to other.
    -- Add Your Signature Here --

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Thanks guys, it's all working. Guess it isn't really that hard if you think about it

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Just do str[100] = '\0';

    It's that simple. If the string is shorter than 100 chars then it won't have any effect on the string, but if it's longer than 100 chars it will cut it off there. Just make sure that str is at least 101 elements big.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by lucaspewkas
    Is there a function that will, if a string has more that 100 chars in it, just cut off anything after the 100th char?

    If not, any hints on how to start writing this function myself?

    Thanks
    Code:
    char *strip_after(char *buf, size_t stripindex)
    {
      buf[min(strlen(buf), stripindex)] = '\0';
    
      return buf;
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
      buf[min(strlen(buf), stripindex)] = '\0';
    
      return buf;
    Bad idea -
    1. C doesn't support variable length arrays (yet)
    2. You can't return a pointer to an array (the array is out of scope when the function exits), so a pointer to it is invalid.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Hm?
    Code:
    char *strip_after(char *buf, size_t stripindex)
    {
      buf[min(strlen(buf), stripindex)] = '\0';
    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.*

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    27

    Be Careful

    Hi,

    Just do str[100] = '\0'; ///?????

    This is not a good idea unless you KNOW that at least 101 char's have
    been allocated for the string.

    You might be putting the '\0' in the middle of another string or
    variable.

    Pappy
    Pappy
    You learn something new everyday.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Oops - my bad
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Pappy1942
    Hi,

    Just do str[100] = '\0'; ///?????

    This is not a good idea unless you KNOW that at least 101 char's have
    been allocated for the string.

    You might be putting the '\0' in the middle of another string or
    variable.

    Pappy
    Did you actually read my whole post?
    Quote Originally Posted by itsme86
    Just make sure that str is at least 101 elements big.
    Do you realize that strncpy(dest, src, 100); has the exact same issue? You always have to make sure the destination is big enough. My method isn't any less safe than any of the others.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Salem
    Code:
      buf[min(strlen(buf), stripindex)] = '\0';
    
      return buf;
    Bad idea -
    1. C doesn't support variable length arrays (yet)

    eh? where is the VLA?

    2. You can't return a pointer to an array (the array is out of scope when the function exits), so a pointer to it is invalid.
    I often do this so the function can be used in expressions. Up to the caller if he uses the return value. Of course you can't assign the return value to an array.

    btw, better use this:

    size_t len = strlen(buf);
    buf[min(len, stripindex)] = '\0';

    the min macro evaluates the arguments twice, so sometimes strlen gets evaluated twice, which is not what you want
    Last edited by Laserve; 04-27-2005 at 11:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM