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
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
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!
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 --
Thanks guys, it's all working. Guess it isn't really that hard if you think about it
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.
Originally Posted by lucaspewkasCode:char *strip_after(char *buf, size_t stripindex) { buf[min(strlen(buf), stripindex)] = '\0'; return buf; }
Bad idea -Code:buf[min(strlen(buf), stripindex)] = '\0'; return buf;
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.
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.*
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.
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.
Did you actually read my whole post?Originally Posted by Pappy1942
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.Originally Posted by itsme86
If you understand what you're doing, you're not learning anything.
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.Originally Posted by Salem
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.