It is possible to copy to a variable the selected index of a string?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 21
Reputation: Rein Valdez has a little shameless behaviour in the past 
Solved Threads: 0
Rein Valdez Rein Valdez is offline Offline
Newbie Poster

It is possible to copy to a variable the selected index of a string?

 
0
  #1
Jan 28th, 2009
Hello Daniweb Forum People, I have this problem of getting the decimal of a number? How can I get it and copy to a variable?

ex.... 1234.59

I used a for loop in order to get the index value of 59, my problem was with the value? how can I transfer this index number to a variable?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: It is possible to copy to a variable the selected index of a string?

 
0
  #2
Jan 28th, 2009
Not a clue what you are talking about.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 21
Reputation: Rein Valdez has a little shameless behaviour in the past 
Solved Threads: 0
Rein Valdez Rein Valdez is offline Offline
Newbie Poster

Re: It is possible to copy to a variable the selected index of a string?

 
0
  #3
Jan 29th, 2009
Im having a problem of getting the values of this string for example

1234.56, I want to extract the 56 in order to have that value. I used a for loop? I am right or their other way how to that?

How can i passed on the value of that extracted to an int one. Thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,036
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: It is possible to copy to a variable the selected index of a string?

 
0
  #4
Jan 29th, 2009
strchr() can find where the period is, the rest is the decimal part.
strtol() can convert the decimal part into a long int.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 21
Reputation: Rein Valdez has a little shameless behaviour in the past 
Solved Threads: 0
Rein Valdez Rein Valdez is offline Offline
Newbie Poster

Re: It is possible to copy to a variable the selected index of a string?

 
0
  #5
Jan 29th, 2009
thank you aia for the answer. Does the For Loop statement needed for this problem?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 21
Reputation: Rein Valdez has a little shameless behaviour in the past 
Solved Threads: 0
Rein Valdez Rein Valdez is offline Offline
Newbie Poster

Re: It is possible to copy to a variable the selected index of a string?

 
0
  #6
Jan 29th, 2009
  1. if((strchr(num,'.')!=0))
  2. {
  3. int cpr,com,hab,con;
  4. cp = strchr(num,'.');
  5. com = cp-num;
  6. hab = strlen(num);
  7. for (cpr=com+1; cpr<=hab;cpr++)
  8. printf("%c",num[cpr]);
  9. //x=num[cpr];
  10. //printf("%c",x);
  11. //con=atoi(num[cpr]);
  12. printf("Print if got decimal");
  13.  
  14. }

This is my code snippet any help in order to get the decimal for example of this no. 1234.56? thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,036
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: It is possible to copy to a variable the selected index of a string?

 
0
  #7
Jan 29th, 2009
strchr() returns a pointer to the first occurrence of the character you want, not to an int.

A call to strtol() will convert string number value representation to long int.
Learn about strtol() over here.
Last edited by Aia; Jan 29th, 2009 at 6:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: It is possible to copy to a variable the selected index of a string?

 
0
  #8
Jan 31st, 2009
If you just want to extract the index (part after the decimal pointer) you can use this method also (in case your index part is in integer bounds)
If you have scanned it as a floating point number:

1>Convert it into a STRING.
2>Then use standard function strtok() with the delimiter as "." and get the second token.
3>This is your index but in string format.
4>Convert it into an integer number.

If you have taken the input as string itself and want the index to be in string format only then disregard step 1 and 4.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 13
Reputation: rajenpandit is an unknown quantity at this point 
Solved Threads: 1
rajenpandit rajenpandit is offline Offline
Newbie Poster

Re: It is possible to copy to a variable the selected index of a string?

 
0
  #9
Feb 2nd, 2009
Sorry Aia actually i m not getting the concept of

cp = strchr(num,'.');
com = cp-num;

can u plz explain for me.........
thanx
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: It is possible to copy to a variable the selected index of a string?

 
0
  #10
Feb 2nd, 2009
That's not Aia's code, and it seems to be incorrect. Here's a working example. It lacks proper error checking for strtol, though. See the link in Aia's post above for the scoop on strtol.
  1. int main (void)
  2. {
  3. char *number = "1234.56";
  4. char *decimal;
  5. long int fraction = 0;
  6.  
  7. if ((decimal = strchr (number, '.')) != NULL)
  8. fraction = strtol (decimal + 1, NULL, 10);
  9.  
  10. printf ("%ld", fraction);
  11. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC