Hi there, I have a string that contains ';r3';. I want to just grab the 3 out of the string and return the decimal value of 3. How would I go about doing that?
Thanks!How to pick a decimal integer value contained in a string and print out its real decimal value in C?
If you are going to have different length strings or ones that you will not know the positions of the values you will need to go through each line of data one character at a time testing to see if is a char or number.
You can use ischar to test if it a character or a number.
If it is a number convert it using atoi.
int mynumber;
int counter = 0;
char testcharacter;
//string or array declared here
while(more string) // psuedo coded
{
read 1 char from string into testcharacter
if(ischar(testcharacter)) // if true it is character, continue
else mynumber = atoi(testcharacter)
}
Hope this helps.
How to pick a decimal integer value contained in a string and print out its real decimal value in C?
int val;
sscanf(';r3';, ';r%d';, %26amp;val);
3 will now be in val. scanf is powerful, and can do more complicated parsing.
No comments:
Post a Comment