I have a string
str = "hello"
I want to make a new string which is the first two digits of str
"he".
How do I do this in C?
I have a string
str = "hello"
I want to make a new string which is the first two digits of str
"he".
How do I do this in C?
Use strncpy(), like this:
#include <stdio.h>
#include <string.h>
int main(void) {
char src[] = "hello";
char dest[3]; // Two characters plus the null terminator
strncpy(dest, &src[0], 2); // Copy two chars, starting from first character
dest[2] = '\0';
printf("%s\n", dest);
return 0;
}
Output:
he