home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CSAPE32.ARJ
/
SOURCE
/
FUNCS
/
FNCOMMA.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-06-14
|
2KB
|
125 lines
/*
fncomma.c 11/18/86
% strcomma, strnocomma
Routines for using commas in numeric fields.
C-scape 3.2
Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
ALL RIGHTS RESERVED.
Revision History:
-----------------
5/13/88 jmd removed length restrictions
12/13/88 jmd removed nasty strchr
6/01/89 gam added ocountry stuff
3/28/90 jmd ansi-fied
6/13/90 jdc replace backwards pointer math with integer math
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "cscape.h"
#include "fnfunc.h" /* for field functions */
char *strcomma(char *s)
/*
Adds appropriate commas to a numeric string.
Everything to right of the (first) decimal point is ignored.
[ 123456789.3333]
becomes: [ 123,456,789.3333]
*/
{
int count, i, j;
char first;
/* remember first character */
first = *s;
/* find end of string or first decimal point */
for (i = 0; s[i] != ocountry.dec_char && s[i] != '\0'; i++) {
;
}
/* move through the string adding commas (removing old ones as we go) */
count = 0;
while (i >= 0) {
if ((i > 0) && isdigit(s[i])) {
count++;
if ((count % 3) == 0 && isdigit(s[i - 1])) {
/* slide over front of string (left), insert a comma. */
i--; /* move to comma's slot */
for (j = 0; j < i; j++) {
s[j] = s[j + 1];
}
s[j] = ocountry.sep_char;
}
i--;
}
else if (s[i] == ocountry.sep_char) {
if ((i > 0) && (count > 0) && (count % 3) == 0 && isdigit(s[i - 1])) {
/* comma belongs */
i--;
}
else {
/* remove old comma */
/* slide over front of string (right), insert a space at the beginning */
for (j = i; j > 0; j--) {
s[j] = s[j - 1];
}
*s = ' ';
}
}
else {
i--;
}
}
/* restore first char if possible */
if (*s == ' ') {
*s = first;
}
return(s);
}
char *strnocomma(char *s)
/*
Removes commas from a string.
Contracts strings towards the right.
[ 123,456,789]
becomes: [ 123456789]
*/
{
int i, j;
/* move through the string removing commas */
for (i = strlen(s) - 1; i >= 0; ) {
if (s[i] == ocountry.sep_char) {
/* slide over front of string, insert a space at the beginning */
for (j = i; j > 0; j--) {
s[j] = s[j - 1];
}
*s = ' ';
}
else {
i--;
}
}
return(s);
}