Jump to page: 1 2
Thread overview
Sorting strings!
Jun 14, 2002
Andrew Edwards
Jun 14, 2002
Burton Radons
Jun 14, 2002
Burton Radons
Jun 14, 2002
Andrew Edwards
Jun 14, 2002
Pavel Minayev
Jun 14, 2002
Andrew Edwards
Jun 14, 2002
Pavel Minayev
Jun 19, 2002
Andrew Edwards
Jun 20, 2002
Pavel Minayev
Jun 20, 2002
Andrew Edwards
Jun 20, 2002
Pavel Minayev
Jun 20, 2002
Sean L. Palmer
Jun 20, 2002
Andrew Edwards
Jun 20, 2002
Pavel Minayev
Jun 20, 2002
Andrew Edwards
Jun 20, 2002
Andrew Edwards
Jun 21, 2002
Pavel Minayev
Jun 24, 2002
Andrew Edwards
Jun 24, 2002
Carlos
June 14, 2002
I'm trying to sort an array and display the result,
then display a tally of each character in the string.
tried using string.sort property but I'm at a lost.
Also, what is the exact meaning of %s?

Thanks,
Andrew
-----------------------------
int main(char[][] args)
{
   char[] where = "Where in D world is C++?";
   char[] why = " And why are you still using it?";
   char[] question;

   question = where~why;
   printf("This is a string, and so is the one below.\n");
   printf(question);
   printf("\n");
   printf("Which has %d characters.\n", question.length);
   return 0;
}


June 14, 2002
Andrew Edwards wrote:
> I'm trying to sort an array and display the result,
> then display a tally of each character in the string.
> tried using string.sort property but I'm at a lost.

Congratulations, you found a bug.  For Walter:

    char [] array = "foobar";

    array.sort; /* Doesn't modify the array */

Also:

    void b() { int[] a = [1, 2]; }

Provokes "Assertion failure: 'ie' on line 253 in file 'declaration.c'".  Putting the declaration outside of the function doesn't provoke the error.  Finally, int [char] and int [wchar] don't work properly... more details on request.

Back to Andrew.  If you're also having trouble with the tallying, the ideal way to do this is through associative arrays.  We're pooling the chars into little piles - a task well suited for the facility.  Pass the string through the function:

    int [int] tallyho (char [] string)
    {
        int [int] array;

        for (int c = 0; c < string.length; c ++)
            array [string [c]] ++;

        return array;
    }

You can then do:

    int [int] tally = tallyho (string);
    int [] keys = tally.dup.keys;

    keys.sort;
    for (c = 0; c < keys.length; c ++)
        printf ("  ch=%c count=%d\n", keys [c], tally [keys [c]]);

The ".dup" in the keys declaration may be unnecessary, but I don't know if modifying the dynamic keys array will cause the universe to implode eventually.  But if you don't sort the keys, they'll come out quite random.

> Also, what is the exact meaning of %s?

Do you mean when I referred to it?  I was just meaning that if my idea were enacted, you wouldn't generally need any other formatting character - you could have, say:

   printf ("%s %s %s", "hello", 4, 7.6);

There are technical reasons for not doing this so I probably shouldn't have mentioned it.

June 14, 2002
Burton Radons wrote:
> You can then do:
> 
>     int [int] tally = tallyho (string);
>     int [] keys = tally.dup.keys;

I'm sorry, I mean tally.keys.dup here.

June 14, 2002
Andrew wrote:

> Also, what is the exact meaning of %s?

"Burton Radons" wote:

| Do you mean when I referred to it?  I was just meaning that if my idea
| were enacted, you wouldn't generally need any other formatting character
| - you could have, say:
|
|     printf ("%s %s %s", "hello", 4, 7.6);

I wasn't referring to that thread, but rather trying to nail down an exacet meaning for %s as defined in D. I'm under the impression that it means print string but everytime I use it I get unexpected results. Maybe I'm using it incorrectly? See line 26 in attachment.

| There are technical reasons for not doing this so I probably shouldn't | have mentioned it.

What were those reasons again? I think it's a great Idea!


June 14, 2002
Some additional questions:

Say I establish the string: char[] myString
how do I restrict it's size to exactly what the user inputs?

Additionally, what is the standard input function for D? scanf()?

June 14, 2002
"Andrew Edwards" <crxace13@comcast.net> wrote in message news:aedfq2$i58$1@digitaldaemon.com...

> I wasn't referring to that thread, but rather trying to nail down an
exacet
> meaning for %s as defined in D. I'm under the impression that it means print string but everytime I use it I get unexpected results. Maybe I'm using it incorrectly? See line 26 in attachment.

Whenever you want to print a D string (char[]), you need to use %.*s %s means a C-style, null-terminated string.



June 14, 2002
"Andrew Edwards" <crxace13@comcast.net> wrote in message news:aedge3$imo$1@digitaldaemon.com...

> Some additional questions:
>
> Say I establish the string: char[] myString
> how do I restrict it's size to exactly what the user inputs?

There is no way to do it using the C scanf(), but if you use my stream.d
module,
you can use stdin.scanf() and the %.*s format specifier. It reads the string
of
unlimited length, and stores it into a char[], modifying length as needed:

    import stream;

    int main()
    {
        char[] name;
        stdout.printf("Enter your name: ");
        stdin.scanf("%.*s", name);
        stdout.printf("Hello, %.*s!", name);
        return 0;
    }

> Additionally, what is the standard input function for D? scanf()?

There's no standard input (as well as output) in D yet. You can rely on
printf()
and scanf() being there, sure, but I guess these won't be the ones you use
in final version of the language. While printf() covers most needs, scanf()
cannot
even read D strings, and I had to rewrite it from scratch just to achieve
this!


June 19, 2002
|     import stream;
|
|     int main()
|     {
|         char[] name;
|         stdout.printf("Enter your name: ");
|         stdin.scanf("%.*s", name);
|         stdout.printf("Hello, %.*s!", name);
|         return 0;
|     }
|

Thanks Pavel! Sorry for not getting back to this earlier!
Just tried to run this snip and was unsuccessful!
user input is requested but not accepted.

Output looks like this:

Enter your name: Andrew Edwards
Hello, !

?????

June 20, 2002
On Wed, 19 Jun 2002 16:54:04 -0400 "Andrew Edwards" <crxace13@comcast.net> wrote:

> |     import stream;
> |
> |     int main()
> |     {
> |         char[] name;
> |         stdout.printf("Enter your name: ");
> |         stdin.scanf("%.*s", name);
> |         stdout.printf("Hello, %.*s!", name);
> |         return 0;
> |     }
> |
> 
> Thanks Pavel! Sorry for not getting back to this earlier!
> Just tried to run this snip and was unsuccessful!
> user input is requested but not accepted.

Yep, my mistake. When you read D strings, you must pass
an _address_ to a D string, and not the string itself. So:

	stdin.scanf("%.*s", &name);	// note the &

Now it works!
June 20, 2002
there's another problem,
running the program with stdin & stdout generate the following errors:

no property 'printf' for type '_iobuf'
no property 'scanf' for type '_iobuf'

so I take them out and I get the following:

Enter your name: Andrew Edwards
Hello, !

I've imported the following library files:

c.stdio
c.stdlib
stream

Program does not work with or without them.

"Pavel Minayev" <evilone@omen.ru> wrote in message
news:CFN374275326275694@news.digitalmars.com...
On Wed, 19 Jun 2002 16:54:04 -0400 "Andrew Edwards" <crxace13@comcast.net>
wrote:

> |     import stream;
> |
> |     int main()
> |     {
> |         char[] name;
> |         stdout.printf("Enter your name: ");
> |         stdin.scanf("%.*s", name);
> |         stdout.printf("Hello, %.*s!", name);
> |         return 0;
> |     }
> |
>
> Thanks Pavel! Sorry for not getting back to this earlier!
> Just tried to run this snip and was unsuccessful!
> user input is requested but not accepted.

Yep, my mistake. When you read D strings, you must pass
an _address_ to a D string, and not the string itself. So:

stdin.scanf("%.*s", &name); // note the &

Now it works!

« First   ‹ Prev
1 2