Java->C I need help
Link |
by shadybones
on 2006-09-17 20:44:27
|
I'm in a class making me program in C, however I only know java. So I'm having to learn C in a day or two to turn in my assignment. SO here are my questions: I use .equals() alot for comparing my strings, what is the equivalent in C? Is == the same? how about string.indexOf(char)? I've defined a Token object: class Token{ int x; void Token{}; String toString(){}; etc..} what is the equivalent in C? Any help would save me a lot of time and I'd appriciate it. Thanks, J |
Re: Java->C I need help
Link |
by
on 2006-09-17 21:53:17 (edited 2006-09-17 21:54:25)
|
Java: String s1 = "string 1"; String s2 = "string 2"; if(s1.equals(s2)); C: char* s1 = "string 1"; char* s2 = "string 2"; if(strcmp(s1, s2) == 0); similarly, you can use strstr to get the same functionality as indexOf() for reading tokens, you can use the strtok function. |
Re: Java->C I need help
Link |
by shadybones
on 2006-09-17 23:06:42
|
thanks for the help, but when I was talking about the Token class, I was referring to any old class that you define yourself in your source file, this one just happens to have the name Token, not the string token class that you're thinking about. Sorry for not clarifying earlier. |
Re: Java->C I need help
Link |
by
on 2006-09-18 00:04:36
|
oh, im not sure about the other Token. was the other stuff helpful? |
Re: Java->C I need help
Link |
by shadybones
on 2006-09-18 16:14:33
|
Yes, the other stuff was exactly what I needed. As for the token thing: class myClass{ int a; myClass(int b){ a=b; } String toString(){ return "" + a; } } Could you rewrite that in C for me? |
Re: Java->C I need help
Link |
by
on 2006-09-18 23:07:09 (edited 2006-09-18 23:10:30)
|
class myClass // C++ { private: // keep member variables private int a; public: // constructor and public functions below myClass(int b) { a = b; } char* toString() { return "" + a; } }; // notice the semicolon! class myClass // Java
{
int a;
myClass(int b)
{
a = b;
}
String toString()
{
return "" + a;
}
} |
Re: Java->C I need help
Link |
by calamity58
on 2006-09-19 00:36:56
|
C/C++ Preprocessor Reference Token-Pasting Operator (##) The double-number-sign or "token-pasting" operator (##), which is sometimes called the "merging" operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token and therefore cannot be the first or last token in the macro definition. If a formal parameter in a macro definition is preceded or followed by the token-pasting operator, the formal parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not performed on the argument prior to replacement. Then, each occurrence of the token-pasting operator in token-string is removed, and the tokens preceding and following it are concatenated. The resulting token must be a valid token. If it is, the token is scanned for possible replacement if it represents a macro name. The identifier represents the name by which the concatenated tokens will be known in the program before replacement. Each token represents a token defined elsewhere, either within the program or on the compiler command line. White space preceding or following the operator is optional. This example illustrates use of both the stringizing and token-pasting operators in specifying program output: #define paster( n ) printf( "token" #n " = %d", token##n ) int token9 = 9; If a macro is called with a numeric argument like paster( 9 ); the macro yields printf( "token" "9" " = %d", token9 ); which becomes printf( "token9 = %d", token9 ); btw.. ill help you for that your request for java >__< |
Re: Java->C I need help
Link |
by
on 2006-09-19 00:45:26
|
please use links to avoid plagiarizing content. i don't think thats what he was looking for, anyway. |
Re: Java->C I need help
Link |
by calamity58
on 2006-09-19 00:56:58
|
im sorry gendou-sama T_T thanks for the rules anyway.. btw... have you visit your thread Be Gendou's New Japanese Best Friend thanks for the rules and warnings arigato ^^V *smile to gendou-sama* |