473,473 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

58 New Member
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
12 1582
arnaudk
424 Contributor
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
donbock
2,426 Recognized Expert Top Contributor
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
sivadhanekula
58 New Member
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
sivadhanekula
58 New Member
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
mac11
256 Contributor
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
JosAH
11,448 Recognized Expert MVP
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
sivadhanekula
58 New Member
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
JosAH
11,448 Recognized Expert MVP
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
sivadhanekula
58 New Member
So, In place of X and Y can we keep '.' and ','. By initializing dot to X and Comma to Y.
Nov 18 '08 #10
JosAH
11,448 Recognized Expert MVP
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
sivadhanekula
58 New Member
I didn't understand the for loop in the given program....Can you explain it for me....

Regards
Siva
Nov 18 '08 #12
JosAH
11,448 Recognized Expert MVP
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

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

Similar topics

2
by: jn | last post by:
I have a link that must have a GET variable with spaces in it. PHP keeps inserting the session id right in the middle of my links. I've tried replacing all spaces with %20...it still sticks it in...
5
by: Sorby | last post by:
Hi I've been coding in PHP for a little while now and was starting to feel pretty confident but then realise I need to understand regular expressions to solve a particular problem I've got ......
13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
7
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
1
by: WT | last post by:
Hello, I was using hastable to store collection of objects of the same class MyClass. I start replacing this hastable by a Dictionary<string,MyClass>....but I was storing this hastable in cache...
6
by: saif.shakeel | last post by:
Hi, I need to replace a string in xml file with something else.Ex - <SERVICEPARAMETER id="_775" Semantics="subfunction" DDORef="_54"> <SHORTNAME>rate</SHORTNAME> <LONGNAME>rate</LONGNAME>...
1
by: patelgaurav85 | last post by:
Hi, I want to convert xml in one format into another xml format shown below Input xml : <Name> <Name1> <Name11>Name11</Name11> <Name12>Name12</Name12>
1
by: Matt Herzog | last post by:
Hey All. I'm learning some python with the seemingly simple task of updating a firewall config file with the new IP address when my dhcpd server hands one out. Yeah, I know it's dangerous to edit...
1
by: gskbond | last post by:
I have a template file whose line reads as: CREATE TABLESPACE "CUSTTBS" LOGGING DATAFILE '{DBFILE_PATH}cust.dbf' SIZE 1000M REUSE AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.