Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,181 questions

56,073 answers

573 users

How to extract the digits of a floating-point number before and after the decimal point from a string in C

3 Answers

0 votes
#include <stdio.h>
#include <string.h>

/*
    split_and_print
    ----------------
    A helper function that receives a string and a delimiter,
    then prints each token on its own line.

    It uses strtok(), which modifies the input string,
    so callers should pass a mutable buffer.
*/
void split_and_print(char *s, const char *delim) {
    char *part = strtok(s, delim);

    while (part != NULL) {
        printf("%s\n", part);
        part = strtok(NULL, delim);
    }
}

int main(void)
{
    char s[] = "3.14";

    // Call the helper function to print each part
    split_and_print(s, ".");

    return 0;
}


/*
run:

3
14

*/

 



answered Jul 29, 2017 by avibootz
edited 17 hours ago by avibootz
0 votes
#include <stdio.h>
#include <string.h>

/*
    print_two_parts
    ----------------
    Splits the string into two parts using strtok()
    and prints each part on its own line.

    This function assumes the string contains exactly
    one delimiter and therefore exactly two parts.
*/
void print_two_parts(char *s, const char *delim) {
    char *part;

    part = strtok(s, delim);
    printf("%s\n", part);

    part = strtok(NULL, delim);
    printf("%s\n", part);
}

int main(void)
{
    char s[] = "3.14";

    // Call the helper function
    print_two_parts(s, ".");

    return 0;
}


/*
run:

3
14

*/

 



answered Jul 29, 2017 by avibootz
edited 17 hours ago by avibootz
0 votes
#include <stdio.h>
#include <string.h>
#include <ctype.h>

/*
    extract_parts
    -------------
    Splits a floating‑point number stored as a string into:
      - digits before the decimal point
      - digits after the decimal point

    The function:
      1. Uses strtok() to split on '.'
      2. Filters out only digit characters from each part
      3. Stores results in caller‑provided buffers

    Assumes the input contains exactly one decimal point.
*/
void extract_float_digits(char *s, char *before, char *after) {
    char *left  = strtok(s, ".");
    char *right = strtok(NULL, ".");

    size_t bi = 0, ai = 0;

    // Copy only digits from the left side
    for (size_t i = 0; left[i] != '\0'; i++) {
        if (isdigit((unsigned char)left[i])) {
            before[bi++] = left[i];
        }
    }
    before[bi] = '\0';

    // Copy only digits from the right side
    for (size_t i = 0; right[i] != '\0'; i++) {
        if (isdigit((unsigned char)right[i])) {
            after[ai++] = right[i];
        }
    }
    after[ai] = '\0';
}

int main(void)
{
    char s[] = "c/c++ c#893725.1046java python";

    // Buffers large enough for digits
    char before[32] = "";
    char after[32] = "";

    extract_float_digits(s, before, after);

    printf("Before decimal: %s\n", before);
    printf("After decimal:  %s\n", after);

    return 0;
}


/*
run:

Before decimal: 893725
After decimal:  1046

*/

 



answered 16 hours ago by avibootz
edited 16 hours ago by avibootz
...