# Thread: Taking only first 100 chars of a string

1. ## 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. 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.

3. 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.

4. Thanks guys, it's all working. Guess it isn't really that hard if you think about it

5. 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.

6. 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. 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.

8. Hm?
Code:
```char *strip_after(char *buf, size_t stripindex)
{
buf[min(strlen(buf), stripindex)] = '\0';```

9. ## 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

11. 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?
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.

12. Originally Posted by Salem
Code:
```  buf[min(strlen(buf), stripindex)] = '\0';

return buf;```
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