Home > other >  Inserting commas in numbers using for loop not working
Inserting commas in numbers using for loop not working

Time:11-20

I tried making a program where it will put a comma in a certain place between numbers. My explanation may be is vague, but I just wanted to make 12345678.23 into 12,345,678.23. I hope that cleared my explanation tho. Here is my code.

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

void main()
{
  char m[20]="12345678.23";
    int j=11, a, t=1, r=4, s;
    
    
        for(a=0; a=11; a  )
        {
            if(strlen(m)==j)
            {printf("%c", m[a]);
            if(a==t)
            {printf(",");}
            if(a==r)
            {printf(",");}
            }   
        }
        
    
}

This program doesn't work and I don't know why. I hope you guys can help me. Thank you very much!

CodePudding user response:

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

int main(void) // Use the proper signature
{
    char m[] = "123"; // Do not hardcode the size
    char *p; // A pointer to traverse the string
    int mod; // Useful to know when to insert a comma

    p = strchr(m, '.'); // Position of the decimal separator
    if (p == NULL)
    {
        p = strchr(m, '\0'); // Don't have decimal separator
    }
    mod = (p - m) % 3; // Comma must be placed on each mod count
    p = m; // Rewind the pointer
    while (*p != '\0') // While the character is not NUL
    {
        if (*p == '.') // Decimal separator reached
        {
            mod = 3; // Avoid divisibility
        }
        // If position divisbile by 3 but not the first pos
        if ((p != m) && (mod != 3) && (((p - m) % 3) == mod))
        {
            putchar(',');
        }
        putchar(*p);
        p  ; // Next character
    }
    putchar('\n');
    return 0;    
}

Output:

12,345,678.23

CodePudding user response:

I actually got it by accident. Here is the new code

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

void main()
{
char m[12];
gets(m);
int j, a, t=1, r=4;
for(j=11; j>=0; j--)
    { 
        for(a=0; a<=j; a  )
        {
            if(strlen(m)==j)
            {
                printf("%c", m[a]);
                if(a==t)
                {
                printf(",");
                }
                if(a==r)
                {
                printf(",");
                }
            }   
        }
        t--;
        r--;
    }
}

the input needs to have at least 2 decimal places at the end, like 1234.34 or 12345678.87.

CodePudding user response:

Would you please try the following:

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

int main()
{
    char m[]="12345678.23";
    int len = strlen(m);                // length of string "m"
    char *p = strchr(m, '.');           // pointer to the decimal point
    int dp;                             // length of the decimal position (including the dot)

    if (p == NULL) dp = 0;              // decimal point does not appear
    else dp = len - (int)(p - m);       // length of the decimal position

    for (int i = 0; i < len; i  ) {     // loop over the characters of "m"
        putchar(m[i]);                  // print the character at first
        if ((len - dp - i - 1) % 3 == 0 && i < len - dp - 1) putchar(',');
                                        // print the comma every three digits from the 1's place backward
    }
    putchar('\n');

    return 0;
}

It works with or without the decimal place. Besides the positions of the comma are automatically calculated. You do not have to explicitly assign them.

  • Related