Thread overview
please correct my char count program...
Jun 20, 2007
Daniel Keep
Jul 03, 2007
davidl
Jun 20, 2007
Derek Parnell
June 20, 2007
here is my count char program:

import std.stdio;
import std.string;

void main()
{
     char* s;
     char string[100];

writef("please input your text=");
scanf("%s",&string);
writef("your text length is=",string.length);
}

does my program work properly?. when i try to run it, d compiler tell that
can't convert string to int. please show me the way to convert string into integer. (if you don't mind i need your correction in my simple char program). thank you very much...GBU
June 20, 2007
import std.stdio : readln, writef, writefln;

void main()
{
    writef("Please input your text: ");
    auto s = readln();
    writefln("Your text length is: %s", s.length);
}

Note that strings in D are *not* NUL-terminated like they are in C. Your code wouldn't work since the .length of a char[100] is *always* 100.  The following is untested, but should work:

import std.string : toString;

void main()
{
    char[100] c_string_buffer;
    scanf("%s", c_string_buffer.ptr);
    auto d_string = toString(c_string_buffer.ptr);
}

.ptr gives you a pointer to an array's first element, so (char[100]).ptr
is a char*.

In this case, toString(char*) assumes its argument is a C-style NUL-terminated string, and converts it to a D string.  Note that it will return a slice of the input data, so that in this particular case, d_string must not outlive c_string_buffer.

	-- Daniel
June 20, 2007
On Tue, 19 Jun 2007 21:48:06 -0400, pieter Valdano pattiruhu wrote:

> here is my count char program:
> 
> import std.stdio;
> import std.string;
> 
> void main()
> {
>      char* s;
>      char string[100];
> 
> writef("please input your text=");
> scanf("%s",&string);
> writef("your text length is=",string.length);
> }

The main problem is that you are trying to write D as if it was C. There are large differences that make D coding a whole lot easier.

For example, here is a way of doing it :

  import std.stdio;

  void main()
  {
    char[] s;

    writef("please input your text=");
    s = readln();
    writef("your text length is=",s.length);
  }

The differences are that D can use a variable-length array rather than a fixed-length one that one typically uses in C. Also in D, it is rare to use pointers such as 'char *p' because it knows a lot more about arrays and pointer usage than C does, which means you don't have to code as much.

Note that when readln() runs, it collects all the characters typed including the Return/Enter key so that the last character in the buffer is always '\n'.


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
20/06/2007 12:13:29 PM
June 20, 2007
Daniel Keep Wrote:

> 
> import std.stdio : readln, writef, writefln;
> 
> void main()
> {
>     writef("Please input your text: ");
>     auto s = readln();
>     writefln("Your text length is: %s", s.length);
> }
> 
> Note that strings in D are *not* NUL-terminated like they are in C. Your code wouldn't work since the .length of a char[100] is *always* 100.  The following is untested, but should work:
> 
> import std.string : toString;
> 
> void main()
> {
>     char[100] c_string_buffer;
>     scanf("%s", c_string_buffer.ptr);
>     auto d_string = toString(c_string_buffer.ptr);
> }
> 
> .ptr gives you a pointer to an array's first element, so (char[100]).ptr
> is a char*.
> 
> In this case, toString(char*) assumes its argument is a C-style NUL-terminated string, and converts it to a D string.  Note that it will return a slice of the input data, so that in this particular case, d_string must not outlive c_string_buffer.
> 
> 	-- Daniel

thank you very much mr.daniel...God Bless U

July 03, 2007
It's really bad to teach someone write buffer overflow vulnerable code

>
> import std.stdio : readln, writef, writefln;
>
> void main()
> {
>     writef("Please input your text: ");
>     auto s = readln();
>     writefln("Your text length is: %s", s.length);
> }
>
> Note that strings in D are *not* NUL-terminated like they are in C.
> Your code wouldn't work since the .length of a char[100] is *always*
> 100.  The following is untested, but should work:
>
> import std.string : toString;
>
> void main()
> {
>     char[100] c_string_buffer;
>     scanf("%s", c_string_buffer.ptr);
>     auto d_string = toString(c_string_buffer.ptr);
> }
>
> .ptr gives you a pointer to an array's first element, so (char[100]).ptr
> is a char*.
>
> In this case, toString(char*) assumes its argument is a C-style
> NUL-terminated string, and converts it to a D string.  Note that it will
> return a slice of the input data, so that in this particular case,
> d_string must not outlive c_string_buffer.
>
> 	-- Daniel



-- 
使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/