Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Does C have anything like the “substr\ (extract substring) routine present in other languages?

0
Posted

Does C have anything like the “substr\ (extract substring) routine present in other languages?

0

Not as such. (One reason it doesn’t is that, as mentioned in question 7.2 and section 8, C has no managed string type.) To extract a substring of length LEN starting at index POS in a source string, use something like char dest[LEN+1]; strncpy(dest, &source[POS], LEN); dest[LEN] = ‘\0’; /* ensure \0 termination */ or, using the trick from question 13.2, char dest[LEN+1] = “”; strncat(dest, &source[POS], LEN); or, making use of pointer instead of array notation, strncat(dest, source + POS, LEN); (The expression source + POS is, by definition, identical to &source[POS] –see also section 6.) comp.lang.c FAQ list ยท Question 13.4 Q: How do I convert a string to all upper or lower case? A: Some libraries have routines strupr and strlwr or strupper and strlower, but these are not Standard or portable. It’s a straightforward exercise to write upper/lower-case functions in terms of the toupper and tolower macros in ; see also question 13.5. (The only tricky part is that the function w

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123