Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

How to get the numbers separately one by one from the following: 20-21-3-1-22-2-4

Thanks in advance..
Posted

You should extract each number individually by using a string tokeniser that will allow you to get the fields one at a time and tell you when there is no more data. You could use the STL basic_string[^] functions, or simply the old C library strtok()[^] function. Or you could write your own specialised function.
 
Share this answer
 
You could use sscanf[^] to parse the string.
C++
char* input = "20-21-3-1-22-2-4";
int num1 = 0, num2 = 0, num3 = 0, num5 = 0, num6 = 0, num7 = 0;

if( sscanf( input, "%d-%d-%d-%d-%d-%d-%d", &num1, &num2, &num3, &num5, &num6, &num7) != 7 )
{
    ;  // Process invalid format

}
 
Share this answer
 
Comments
divirakshana 12-Nov-11 7:53am    
Thats is for example i have told one number but i dont know the result, but i have to get the number separately. What shall i do for that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900