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
Printable View
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.
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.
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.
Quote:
Originally Posted by lucaspewkas
Code: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.
Hm?Code:char *strip_after(char *buf, size_t stripindex)
{
buf[min(strlen(buf), stripindex)] = '\0';
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
Oops - my bad :)
Did you actually read my whole post?Quote:
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.Quote:
Originally Posted by itsme86
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.Quote:
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