IHYPRESS PROGRAMMING
Tutorials and C programs with code and output for beginners
c programming
HOME | ASP | C | CSS | GNUPLOT | 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 IHY PRESS Frankfurt am Main 60329 Deutschland