-
Extracting values...
I have what I believe to be a simple question for those that are experienced. I hope you guys can help me out.
How do you take part of a value and break it up to be used as a different value? For example, Value = California (100) is the value to be used. However, how can I take pieces of that value, such as split up California and 100 to be used somewhere else? Ultimately, I want to be able to take the value up until the parentheses, use that value separately, then take what is actually inside the parentheses, and then use that value independently as well.
I hope this makes sense. Thank you all for your response. And this is not for any kind of assignment whatsoever. I'm just trying to learn the language as part of ongoing work.
Thanks again!
-
I take it "California (100)" is a string. You might want to look at using strchr.
-
Sounds like you've already got it worked out. Go through the input until you reach the '=', and ignore everything before it. Then, go through until you reach '(', copying that some place. Then skip past that, and copy everything until you reach the ')'.
There are many ways to do it. But that's an easy to understand method. Use a couple of arrays to hold what you're copying. This assumes you've got the data some place as a string. Be it a file or an array or what not.
Quzah.
-
in some situations, you can scan using sscanf().
The format string must be carefully prepared though.
For example, this one may work for you:
Code:
const char *str = "Value = California(100)";
char v[100];
int x;
sscanf(str, "%*s = %99[A-Za-z](%d)", v, &x);
printf("[%s] [%d]\n", v, x);
-
Cool. Thanks for all the replies. I really appreciate it!!