Add to Google

Isolating Integers From Characters?

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

Isolating Integers From Characters?

joeandreem
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 50

Anybody got any idea how to do that??
I would really appreciate the help!!
Thanks.

Comment/Reply (w/o sign-up)

jlhaslip
php has a function for comma seperated values. Can you use that?

Comment/Reply (w/o sign-up)

joeandreem
No, I don't think I'm allowed to do that...I should write a simple function that can isolate these integers. I don't think there is a function in stdio.h that does that,is there??

Comment/Reply (w/o sign-up)

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

CODE
<?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 smile.gif

More information ...
QUOTE

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.

 

 

 


Comment/Reply (w/o sign-up)

bidarshi
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.

Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*


Searching Video's for isolating, integers, characters




advertisement



Isolating Integers From Characters?