By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,948 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,948 IT Pros & Developers. It's quick & easy.

C-Code for replacing ',' in place of '.' (i.e. for ex 39.6 to 39,6)

P: 58
Hi everyone

I have a database of 24 tables all with 'dot separated numbers like 39.6' and I am extracted the required data in to one table which is also a dot separated data. But I need that data in comma separated value"i.e. in place of 39.6 i need 39,6"..Since I am in Germany here comma's and dot's are replaced. So can you give me a code so that if there are any dots in my input it should be converted to comma and should give it in my out put.

Thank you
Nov 12 '08 #1
Share this Question
Share on Google+
12 Replies


100+
P: 424
You can use sscanf() and sprintf(), something like this:
Expand|Select|Wrap|Line Numbers
  1. char num[] = "39.6";
  2. int p,d;
  3. sscanf(num,"%i.%i",p,d); // p=39 and d=6
  4. sprintf(num,"%i,%i",p,d); // now num = "36,6"
  5.  
Nov 12 '08 #2

Expert 100+
P: 2,415
Another option is to investigate how locales work. I've never used this feature myself, but my understanding is that if you set the right locale then the numbers will naturally print out with commas instead of periods.
Nov 12 '08 #3

P: 58
can you tell me what a locale is...I have never heard of it and can you tell me how can I check that for solving this problem..
Thank you
Nov 12 '08 #4

P: 58
You can use sscanf() and sprintf(), something like this:
Expand|Select|Wrap|Line Numbers
  1. char num[] = "39.6";
  2. int p,d;
  3. sscanf(num,"%i.%i",p,d); // p=39 and d=6
  4. sprintf(num,"%i,%i",p,d); // now num = "36,6"
  5.  
Thank you.....I have got the output with the above code...but with some changes....
Nov 12 '08 #5

100+
P: 256
can you tell me what a locale is...I have never heard of it and can you tell me how can I check that for solving this problem..
Thank you
donbock is right - locales are the best way to solve your problem. I've used it before in a project that had to support multiple languages and numerical formats (like what you want to do with , and . )

Have a look at these links - I just googled them up but they should give you an idea.

http://www.w3.org/International/questions/qa-i18n
http://www.acm.uiuc.edu/webmonkeys/b...guide/2.6.html
Nov 12 '08 #6

Expert 10K+
P: 11,448
Another option is to investigate how locales work. I've never used this feature myself, but my understanding is that if you set the right locale then the numbers will naturally print out with commas instead of periods.
In an ideal world that would work; in a real world most of the time just the "C"
locale is supported which prints the decimal point as a dot. The solution given
above will work or otherwise the following little hack will do:

Expand|Select|Wrap|Line Numbers
  1. char* replaceXbyY(char* p, char x, char y) {
  2.    char* s;
  3.    for (s= p; s && *s; s++)
  4.       if (*s == x) *s= y;
  5.    return p;
  6. }
  7.  
kind regards,

Jos
Nov 12 '08 #7

P: 58
In an ideal world that would work; in a real world most of the time just the "C"
locale is supported which prints the decimal point as a dot. The solution given
above will work or otherwise the following little hack will do:

Expand|Select|Wrap|Line Numbers
  1. char* replaceXbyY(char* p, char x, char y) {
  2.    char* s;
  3.    for (s= p; s && *s; s++)
  4.       if (*s == x) *s= y;
  5.    return p;
  6. }
  7.  
kind regards,

Jos
Hi...

Can you give me a code for this in for loop 'like' in the for loop it should check all my data and if it is a point that point should be replaced by a comma
Nov 18 '08 #8

Expert 10K+
P: 11,448
Hi...

Can you give me a code for this in for loop 'like' in the for loop it should check all my data and if it is a point that point should be replaced by a comma
No; the little 'replaceXbyY' function replaces all 'x's by 'y's in a string; I leave it
to your imagination how to use it for your output.

kind regards,

Jos
Nov 18 '08 #9

P: 58
So, In place of X and Y can we keep '.' and ','. By initializing dot to X and Comma to Y.
Nov 18 '08 #10

Expert 10K+
P: 11,448
So, In place of X and Y can we keep '.' and ','. By initializing dot to X and Comma to Y.
Sure; have a look at the parameter list: x and y are parameters and the function
replaces the value of every x by the value of y. Feel free to change that little
function.

kind regards,

Jos
Nov 18 '08 #11

P: 58
I didn't understand the for loop in the given program....Can you explain it for me....

Regards
Siva
Nov 18 '08 #12

Expert 10K+
P: 11,448
I didn't understand the for loop in the given program....Can you explain it for me....
Either parameter p is NULL or it isn't. If it is the condition 's && *s' fails immediately
and no loop body is executed. Otherwise pointer s traverses over all characters
pointed to by p and s until it reaches the '\0' character in which case the for loop
condition 's && *s' fails again.

kind regards,

Jos
Nov 18 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.