943,926 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1124
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 28th, 2009
0

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

Expand Post »
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?
Similar Threads
Reputation Points: 5
Solved Threads: 0
Newbie Poster
Rein Valdez is offline Offline
21 posts
since Jan 2009
Jan 28th, 2009
0

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

Not a clue what you are talking about.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,738 posts
since May 2006
Jan 29th, 2009
0

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

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
Reputation Points: 5
Solved Threads: 0
Newbie Poster
Rein Valdez is offline Offline
21 posts
since Jan 2009
Jan 29th, 2009
0

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

strchr() can find where the period is, the rest is the decimal part.
strtol() can convert the decimal part into a long int.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 29th, 2009
0

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

thank you aia for the answer. Does the For Loop statement needed for this problem?
Reputation Points: 5
Solved Threads: 0
Newbie Poster
Rein Valdez is offline Offline
21 posts
since Jan 2009
Jan 29th, 2009
0

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

  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.
Reputation Points: 5
Solved Threads: 0
Newbie Poster
Rein Valdez is offline Offline
21 posts
since Jan 2009
Jan 29th, 2009
0

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

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 31st, 2009
0

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

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Feb 2nd, 2009
0

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

Sorry Aia actually i m not getting the concept of

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

can u plz explain for me.........
thanx
Reputation Points: 9
Solved Threads: 1
Newbie Poster
rajenpandit is offline Offline
13 posts
since Aug 2008
Feb 2nd, 2009
0

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

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. }
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need help with simple C program
Next Thread in C Forum Timeline: Give me a Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC