| | |
It is possible to copy to a variable the selected index of a string?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2009
Posts: 21
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
if((strchr(num,'.')!=0)) { int cpr,com,hab,con; cp = strchr(num,'.'); com = cp-num; hab = strlen(num); for (cpr=com+1; cpr<=hab;cpr++) printf("%c",num[cpr]); //x=num[cpr]; //printf("%c",x); //con=atoi(num[cpr]); printf("Print if got decimal"); }
This is my code snippet any help in order to get the decimal for example of this no. 1234.56? thanks.
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.
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.
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.
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"....
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.
C Syntax (Toggle Plain Text)
int main (void) { char *number = "1234.56"; char *decimal; long int fraction = 0; if ((decimal = strchr (number, '.')) != NULL) fraction = strtol (decimal + 1, NULL, 10); printf ("%ld", fraction); }
![]() |
Similar Threads
- PLS I NEED YOUR HELP! Help on Reading and Editing of already Written CODE on php! (PHP)
- Open In New Window Php (PHP)
- GUI buttons Inventory Part 5 (Java)
- modify script (Perl)
Other Threads in the C Forum
- Previous Thread: Need help with simple C program
- Next Thread: Give me a Problem
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






