how to check for int and ID

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
What are the contents of your input file?
i need to sort each string

void main ( void )
{ int x ;
x = read ( ) ;
if ( x > 0 ) write ( fact ( x ) ) ;
}
What do you mean?
What are the contents of your file?
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
Can someone get me started please !
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
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.
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.