954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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?

Rein Valdez
Newbie Poster
21 posts since Jan 2009
Reputation Points: 5
Solved Threads: 0
 

Not a clue what you are talking about.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

Rein Valdez
Newbie Poster
21 posts since Jan 2009
Reputation Points: 5
Solved Threads: 0
 

strchr() can find where the period is, the rest is the decimal part.
strtol() can convert the decimal part into a long int.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

thank you aia for the answer. Does the For Loop statement needed for this problem?

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

Rein Valdez
Newbie Poster
21 posts since Jan 2009
Reputation Points: 5
Solved Threads: 0
 

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.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

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.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

Sorry Aia actually i m not getting the concept of

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

can u plz explain for me.........
thanx

rajenpandit
Newbie Poster
14 posts since Aug 2008
Reputation Points: 9
Solved Threads: 1
 

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.

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);
}
nucleon
Posting Pro in Training
478 posts since Oct 2008
Reputation Points: 163
Solved Threads: 91
 
startPos = strlen(MyInput);
			do {
		startPos = startPos - 3;
				if (startPos < 0) {
				numdigits = numdigits + startPos;
				startPos = 0;
					}
				strncpy(threedigits, MyInput + startPos, numdigits);
				threedigits[numdigits] = '\0';
			printf("%s ",threedigits);

Another problem of mine, due to editing of the whole program, this code snippet chop the input number by 3 digits. ex. 123456789 will output 789 456 123. How could I get the value first of 789 in the variable threedigits? thanks.

Rein Valdez
Newbie Poster
21 posts since Jan 2009
Reputation Points: 5
Solved Threads: 0
 

>How could I get the value first of 789 in the variable threedigits?

Given:

char number[] = "123456789";
    char *chopchop;
    char threedigits[4] = {'\0'};


Do:

/* start at beginning of wanted digists  */
    chopchop = &number[strlen(number) - 3];

    /* copy wanted digits as a string */
    strcpy(threedigits, chopchop);


include for strlen() and strcpy()

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You