Jump to content
xisto Community
Sign in to follow this  
joeserhal

Isolating Integers From Characters?

Recommended Posts

Hi there people!I'm new here, so if this post conflicts with any of the laws of this forum for any reason, I apologize in advance.I'm trying to write a function that reads from the user a set of integers separated by ',' (commas), but I'm having difficulty isolating the integers from the characters. I'm not sure if I should do it using a "for" or "while" loop,...So if the input is something like: {100,20,50}I should be able to store the integers 100 20 50Anybody got any idea how to do that??I would really appreciate the help!!Thanks.

Share this post


Link to post
Share on other sites

If this is PHP, this is what you can do.

 

<?php // Example 1 $pizza  = "piece1,piece2,piece3,piece4,piece5,piece6"; $pieces = explode(",", $pizza); print $pieces[0]; // piece1 print $pieces[1]; // piece2 // Example 2 $data = "foo:*:1023:1000::/home/foo:/bin/sh"; list($user,$pass,$uid,$gid,$gecos,$home,$shell) = explode(":",$data); print $user; // foo print $pass; // * ?>

The explode function is php is just there for your purpose :(

 

More information ...

Split a string by string (PHP 3, PHP 4 )

 

array explode ( string separator, string string [, int limit] )

 

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string separator. If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

 

If separator is an empty string (""), explode() will return FALSE. If separator contains a value that is not contained in string, then explode() will return an array containing string.

 

Although implode() can, for historical reasons, accept its parameters in either order, explode() cannot. You must ensure that the separator argument comes before the string argument.

 


Share this post


Link to post
Share on other sites

I think you can solve your problem in C or C++ by entering your input data into a character type string. The length of this string you can easily obtain.Now you can extract each individual character from the string. Check each character's ASCII value whether they are between the range for the characters . if this result is true neglect the character and search for the next character. If the checking implies that the extracted character is not a character or a special character then this extracted character is an integer.You store this CHARACTER in a result array.Now if you output the result string you will get the integers entered. In my knowledge there is no readymade function to extract the integers as such and you will have to code it yourself.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.