digitalmars.D.learn - A problem in converting C code
- pascal111 (20/20) Nov 02 2021 In next program I intend it to be the D version of a C program,
- =?UTF-8?Q?Ali_=c3=87ehreli?= (24/25) Nov 02 2021 As you hint, this really is not D but still... :) I had to make three
- =?UTF-8?Q?Ali_=c3=87ehreli?= (14/14) Nov 02 2021 On 11/2/21 3:57 PM, Ali =C3=87ehreli wrote:
- pascal111 (3/17) Nov 02 2021 This is D point of view to the same classic C code. I think D has
- zjh (2/4) Nov 02 2021 this is very interesting .`readln` then `strip;`,Very natural.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (14/22) Nov 02 2021 Yes, UFCS (universal function call syntax) makes code natural, concise,=...
- pascal111 (2/27) Nov 02 2021 Yes, exactly, this is literal converting as most possibility.
In next program I intend it to be the D version of a C program,
but I found some troubles with it, can we keep the C style of it
as it is as we can and fix the must-be-fixed parts to make it
works under D compiler?
// D programming language
import std.stdio;
import core.stdc.stdio;
import core.stdc.string;
int main()
{
char x[20];
fgets(&x[0],x.sizeof,null);
x[strcspn(&x[0],"\n")]=0;
writeln(x);
if(!strcmp(&x[0],"hello world!"))
{
writeln("yes");
}
return 0;
}
Nov 02 2021
On 11/2/21 3:36 PM, pascal111 wrote:can we keep the C style of it as it isAs you hint, this really is not D but still... :) I had to make three changes: import std.stdio; // Ali - Importing stdin under a different name // to prevent name conflict with std.stdio.stdin; import core.stdc.stdio : c_stdin = stdin; import core.stdc.string; int main() { // Ali - Changed the type to char[20] char[20] x; // Ali - The last parameter is the stream // to read from. fgets(&x[0],x.sizeof,stdin.getFP()); x[strcspn(&x[0],"\n")]=0; writeln(x); if(!strcmp(&x[0],"hello world!")) { writeln("yes"); } return 0; } Ali
Nov 02 2021
On 11/2/21 3:57 PM, Ali =C3=87ehreli wrote:
Here is a more idiomatic version:
import std.stdio;
import std.string;
void main() {
const x =3D strip(readln());
writeln(x);
if (x =3D=3D "hello world!") {
writeln("yes");
}
}
The first line in main can be written with UFCS syntax as well:
const x =3D readln.strip;
Ali
Nov 02 2021
On Tuesday, 2 November 2021 at 23:02:42 UTC, Ali Çehreli wrote:
On 11/2/21 3:57 PM, Ali Çehreli wrote:
Here is a more idiomatic version:
import std.stdio;
import std.string;
void main() {
const x = strip(readln());
writeln(x);
if (x == "hello world!") {
writeln("yes");
}
}
The first line in main can be written with UFCS syntax as well:
const x = readln.strip;
Ali
This is D point of view to the same classic C code. I think D has
the spirit of C++.
Nov 02 2021
On Tuesday, 2 November 2021 at 23:02:42 UTC, Ali Çehreli wrote:On 11/2/21 3:57 PM, Ali Çehreli wrote:const x = readln.strip;this is very interesting .`readln` then `strip;`,Very natural.
Nov 02 2021
On 11/2/21 6:49 PM, zjh wrote:On Tuesday, 2 November 2021 at 23:02:42 UTC, Ali =C3=87ehreli wrote:Yes, UFCS (universal function call syntax) makes code natural, concise,=20 and readable (but things can get out of hand :) ). Here is an example copied from the home page of dlang.org: import std.stdio, std.array, std.algorithm; void main() { stdin .byLineCopy .array .sort!((a, b) =3D> a > b) // descending order .each!writeln; } AliOn 11/2/21 3:57 PM, Ali =C3=87ehreli wrote:=20=C2=A0 const x =3D readln.strip;=20 this is very interesting .`readln` then `strip;`,Very natural. =20 =20
Nov 02 2021
On Tuesday, 2 November 2021 at 22:57:14 UTC, Ali Çehreli wrote:On 11/2/21 3:36 PM, pascal111 wrote:Yes, exactly, this is literal converting as most possibility.can we keep the C style of it as it isAs you hint, this really is not D but still... :) I had to make three changes: import std.stdio; // Ali - Importing stdin under a different name // to prevent name conflict with std.stdio.stdin; import core.stdc.stdio : c_stdin = stdin; import core.stdc.string; int main() { // Ali - Changed the type to char[20] char[20] x; // Ali - The last parameter is the stream // to read from. fgets(&x[0],x.sizeof,stdin.getFP()); x[strcspn(&x[0],"\n")]=0; writeln(x); if(!strcmp(&x[0],"hello world!")) { writeln("yes"); } return 0; } Ali
Nov 02 2021









pascal111 <judas.the.messiah.111 gmail.com> 