IHYPRESS.NET
C
HOME | ASP | C | CSS | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
CStrings ❯ Compound Formula Split
<08.06>
// 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); }
Hergestellt in Deutschland / Made in Germany
Enter a compound > C6H13NO2 C6 H13 N O2
COPYRIGHT © 2015-2024 IHYPRESS.NET. A DIVISION OF IHY PRESS. ALL RIGHTS RESERVED.