how to check for int and ID

Oct 2, 2016 at 12:34am
Hello.
I need help with sorting strings that i read from a file, the file contains strings like ("int" , "[" , "]", "x", "1")
i put them into array and now i need to sort them to 3 category arrays :
integers: "1" "2" ... "9"
IDs : "x" "read" "write" ......
keywords: "int" "fact" "void" " "while" " "return" .....

can someone show me some code that can sort the array.
thank you !
Last edited on Oct 2, 2016 at 1:13am
Oct 2, 2016 at 1:00am
What are the contents of your input file?
Oct 2, 2016 at 1:13am
i need to sort each string

void main ( void )
{ int x ;
x = read ( ) ;
if ( x > 0 ) write ( fact ( x ) ) ;
}
Oct 2, 2016 at 1:35am
What do you mean?
What are the contents of your file?
Oct 2, 2016 at 1:45am
Content of File :
"void main ( void )
{ int x ;
x = read ( ) ;
if ( x > 0 ) write ( fact ( x ) ) ;
} "

i need to sort them like :
KeyWord[] = void;
Keyword[] = main;
ID[] = x;
Integer[] = 0;
and so on. but i dont know how to make the program know which is int and which is ID
Last edited on Oct 2, 2016 at 3:57am
Oct 2, 2016 at 6:48am
Can someone get me started please !
Oct 2, 2016 at 10:23am
Term "sort" refers usually to adjusting the order of items in a list.

You have three "buckets" and you want to toss each word into appropriate bucket.
FOR each word
  IF is an integer
  ELSE IF is a keyword
  ELSE

You do need two predicates;
* one that returns true if the word is an integer
* one that returns true if the word is a keyword
Oct 2, 2016 at 2:37pm
True, i just don't know what to write in "If" statement to check for integers and for the others. tried to many ways and it's not working.
Oct 2, 2016 at 3:32pm
Lets take the keywords first. You must have a list of them. Then, if a word is in the list, then the word is a keyword.
For example: http://www.cplusplus.com/reference/set/set/count/

For numbers you could put the word into istringstream, attempt to read the number out and check whether that was all that the word had.
Topic archived. No new replies allowed.