// Using strncpy to separate compounds into elemental components
// Displays each elemental component of a compound
#include <stdio.h>
#include <string.h>
#define CMP_LEN 30
#define ELEM_LEN 10
int
main(void)
{
char compound[CMP_LEN];
char elem[ELEM_LEN];
int current, next;
// Gets data string representing compound
printf("Enter a compound > ");
scanf("%s", compound);
// Displays each elemental component
// identified by an initial capital letter
current = 0;
for (next = 1; next < strlen(compound); ++next)
if (compound[next] >= 'A' && compound[next] <= 'Z') {
strncpy(elem, &compound[current], next - current);
elem[next - current] = '\0';
printf("%s\n", elem);
current = next;
}
// Displays the last component
printf("%s\n", strncpy(elem, &compound[current], ELEM_LEN));
return (0);
}