In C, you can extract a substring from a string using the `strncpy()` function from the `string.h` library. Here is an example:
```
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello, world!";
char str2[100];
strncpy(str2, str1 + 7, 5); // Extract a substring from str1
printf("%s", str2); // Print the extracted substring
return 0;
}
```
In this example, we extract a substring from `str1` starting at index 7 (which is the space character after "Hello,") and with a length of 5 (which includes "world" and the null terminator character). The resulting substring is stored in `str2`. Finally, the extracted substring is printed using the `printf()` function.
Note that `strncpy()` does not automatically add a null terminator character at the end of the extracted substring. If you want to use the extracted substring as a string, you must add a null terminator character manually. In this example, we did not need to add a null terminator character because `str2` was initialized to an empty string, which already has a null terminator character at the end.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc