> I could have sworn that one fizzled out. Oh well.
See. You do this and wonder why the forum is a boring and dull place. :)

I only know VB so here goes:

Dim name As String
Dim length As Integer
Dim Firstname As String
Dim LastName As String
Dim x As Integer 'or is it?
 
name = InputBox("What is your name")
 
length = Len(name)
x = InStr(name, " ")
Firstname = Left(name, x)
LastName = Right(name, length - x)
 
name = LastName & ", " & Firstname
 
MsgBox "The name is " & name & " " & LastName

I am called James Bennet so it goes "the name is Bennet, James Bennet" ( a pun on the old james bond one)

Dim word As String
Dim reverse As String
Dim x As Integer
Dim counter As Integer
Dim length As Integer
 
word = InputBox("Enter a word")
 
length = Len(word)
 
For x = 0 To length - 1
 
reverse = Mid(word, length - x, 1)
Print reverse;
 
Next x

this second example reverses a word - e.g "HELLO" would be "OLLEH"

Member Avatar for iamthwee

Nice one's Jbennet, but you could also have used regular expressions to parse the words in a sentence, which is especially useful if words are separated by more than one space.

viz

>Based on that, here's my third attempt
Close, but you're still treating the arguments as one whole string. You only get them one at a time. For example:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
  typedef string Token;
  
  istringstream tokenizer ( "arg1 arg2 arg3" );
  Token tok;

  while ( tokenizer>> tok ) {
    // TODO: Add the token to the stack frame
  }
}

You're welcome to modify the loop, but the tokenizer is an integral piece of the compiler, so we can't rewrite it to give you all of the arguments as a single string.

Ah, I guess i missed the clue you put inside that loop before - As i think about it, i've been over-complicating it a little.
I decided to approach it, assuming that I knew absolutely nothing about the type of Token ...

template<typename TokenType>
class tokenstream
{
    struct holder_t
    {
        TokenType token;
        holder_t* next;
        holder_t(TokenType t, holder_t* h) : token(t), next(h) {}
    } * head;
public:
    tokenstream() : head(NULL) {}
    ~tokenstream()
    {
        while(head)
        {
            holder_t* temp(head);
            head = head->next;
            delete temp;
        }
    }
    bool add(TokenType);
    TokenType next();
    bool is_empty() { return ( head == NULL ); }
};

template<typename TokenType>
bool tokenstream<TokenType>::add(TokenType t)
{
    head = new holder_t(t, head) ;
    return( head != NULL );
}

template<typename TokenType>
TokenType tokenstream<TokenType>::next()
{
    if( head == NULL )
        throw("tokenstream: token list empty");
    TokenType t( head->token );
    holder_t* temp( head );
    head = head->next;
    delete temp;
    return t;
}

So, based on your code, the loop would look like this

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
  typedef string Token;
  
  istringstream tokenizer ( "arg1 arg2 arg3" );
  Token tok;

  tokenstream<Token> reverser;

  while ( tokenizer>> tok ) {
    reverser.add( tok );
  }
}

Here's a simple in-place (albeit quadratic) one that's more for English words than C++ identifiers.

bool isword(char x) {
  return std::isalnum(x) || x == '_' || x == '\'';
}

// reverses [beg,end)
void reverse(char* beg, char* end) {
  while (beg < --end) {
    char tmp = *beg;
    *beg = *end;
    *end = tmp;
    ++ beg;
  }
}

// reverses words of [beg, end)
void reverse_words(char* beg, char* end) {
  char* b = beg;
  char* e = end - 1;

  while (b < e) {
    while (b < e && ! isword(*b))
      ++b;
    while (e > b && ! isword(*e))
      --e;
    if (b < e) {
      reverse(b, e + 1);
      
      while (++b < e && isword(*b));
      while (b < --e && isword(*e));

      if (b < e) {
	reverse(b, e + 1);
      }
    }
  }

  // Now reverse individual words.

  b = beg;
  e = end;

  while (b < e) {
    while (b < e && ! isword(*b)) {
      ++b;
    }

    if (b == e) {
      break;
    }

    char *wordEnd = b;
    while (++wordEnd < e && isword(*wordEnd));

    reverse(b, wordEnd);

    b = wordEnd;
  }

  return;
}

...

Neither of these have anything to do with the problem at hand...

Member Avatar for iamthwee

Neither of these have anything to do with the problem at hand...

I think he was solving his own lil puzzle.

Those questions aside, here's how i'd do it in C++
I like it. But I forgot to tell you that our development environment is extremely buggy and resource intensive (management is looking into replacing it). Using the standard library would be a bad idea for this component since it'll be used very often during a compilation phase.

Which would lead to the question "if not the standard library, what library are we to use and where can we find the documentation for it?".

But you would have to accept a solution using the standard library given your stated requirements since they lack mention of not being able to use it nor mention the alternative to use ;)

I'd also ask at this stage if this requirements process you're putting up is similar to the normal one in your company, and might well get second thoughts about the whole job if it it.

The discussion might well get to the point where the interview is concluded without any code being actually written (which may or may not influence the outcome of the interview process, depending on the who processes the interview results at your end and how, which could also be a question to ask).

i was just thinking, sorry ;)

commented: I think you need your red thinking cap, omg sorry that's your hair :) +12

I think he was solving his own puzzle.

he was solving a problem in the way he understood it from the original requirements which were incomplete and unclear (though he didn't see them as such apparently so he didn't consider the later clarification).

commented: "incomplete and unclear" which is why you don't let a woman on your software development team! +12

mmm i dont know why people dislike VB for simple tasks like this

because there's nothing to like about VB :)

Member Avatar for iamthwee

because there's nothing to like about VB :)

Incorrect. In fact VB should be particulary close to your heart jwenting.

vb myth

[VB]Is similar to Java(tm) :FALSE, Visual Basic *is* JAVA(tm).

proof

VB is not like java but C# is (c# and java both have roots in C)

That wasnt VB.NET anyway, that was VB classic

I like vb because it is:

easy to understand
easy to learn
good for RAD - you can do in 2 mins on VB what could take you 30mins on C++
relatively fast*

*this is also a con - see below

Cons:

quite fast but not as fast as C and similar languages. In my eperience VB classic is faster than java though (maybe as its unmanaged?)

but for everyday programming tasks VB is just fine so i wish people would stop ridiculing it and calling it a n00b language.

anyway, back to the topic in hand.....

Incorrect. In fact VB should be particulary close to your heart jwenting.

are you deliberately insulting me, boy? Or is it your ignorance talking?

In either case a good spanking is in order to teach you to behave yourself in the future!

VB is not like java but C# is (c# and java both have roots in C)

Aside from dot syntax (which most likely preceeds C), how are they related?

VB reads like a childrens book.

the grammar and structure is poorly created.

DIM foo AS something = something;

or instead of using the "this" keyword it uses ME

ME.foo

(which reads terrible, is is like saying, me contain foo, instead of this contains foo)

instead of NULL or csharps null (just casing difference) VB uses NOTHING

DIM foo as SomeObject = NOTHING;

VB works great as a scripting language, and is implimented well for that, but as a software langugage to handle any task it is a major joke.

Member Avatar for iamthwee

>but as a software langugage to handle any task it is a major joke.
Incorrect.

Visual Basic is considered the most concise and well implemented computer language currently in existence. The purity of VB's syntax and lexical model is often compared to the finest Waterford crystal. Another great feature of Visual Basic is its fast running time when compared with assembly code.

>are you deliberately insulting me, boy?

Read the link sir. Websites don't lie. Vb has been proven to be java in disguise...

I built a perfectly fine:

database system for a local charity
word processor
network chat client

using VB

>but as a software langugage to handle any task it is a major joke.
Incorrect.

i am sure we could both find people to argue both sides of the fence ;)

it's no use for either one of us to argue it, but i am basing this on personal trial/error using VB and VB's basic grammar/structure.

>I decided to approach it, assuming that I knew absolutely nothing about the type of Token ...
Nice. In fact, I'll accept that as a viable solution. :) I also would have accepted a recursive solution:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

typedef string Token;

void push_frame_arg ( Token tok )
{
  cout<< tok <<'\n';
}

bool add_arguments ( istream& tokenizer )
{
  Token tok;

  if ( tokenizer>> tok ) {
    add_arguments ( tokenizer );
    push_frame_arg ( tok );
  }
}

int main()
{
  istringstream tokenizer ( "arg1 arg2 arg3" );
  Token tok;

  add_arguments ( tokenizer );
}

>Which would lead to the question "if not the standard library, what
>library are we to use and where can we find the documentation for it?".
Which would be a good question.

>But you would have to accept a solution using the standard library
>given your stated requirements since they lack mention of
Nope. The point of many of these questions is to figure out exactly what the requirements are before trying to come up with a solution. Vague requirements are very common with clients, and you're basically saying that I (as the client) have to accept your solution as final even if it's your fault that the solution is worthless. How many clients do you think would pay if you tried to pull that?

>But you would have to accept a solution using the standard library
>given your stated requirements since they lack mention of
Nope. The point of many of these questions is to figure out exactly what the requirements are before trying to come up with a solution. Vague requirements are very common with clients, and you're basically saying that I (as the client) have to accept your solution as final even if it's your fault that the solution is worthless. How many clients do you think would pay if you tried to pull that?

Indeed - if this thread has taught me one thing, its to question every word of every sentence, and to question every assumption that results .. I guess the old adage reigns true - You only truly understand the problem around the time you've finished creating the solution :)

Visual Basic is considered the most concise and well implemented computer language currently in existence

By whom? Not by anyone with a grain of intellect...

Member Avatar for iamthwee

Says the net and just like father Christmas and Bill Clinton, the net never lies.

ah, so another fictional delusion?

ah, so another fictional delusion?

basically.

hahah would not be supprised if billy gates himself wrote that.

i'm not a windows basher but billy gates sure does love his visual basic even though it works better for scripting.

commented: Lame. BY READING THIS MESSAGE YOU AGREE TO FORGO ALL RIGHTS OF LIABILITY AND CHILD-BEARING, AND YOU AGREE TO INDEMNIFY Rashakil Fol FROM ALL FORMS OF FLATULENCE. -2
commented: Just correct the misspellings: SOMTHING should be SOMETHING and AMMOUNT should be AMOUNT. +6

i'm not a windows basher but billy gates sure does love his visual basic even though it works better for scripting.

Are you trying to make an ass out of yourself?

VB isnt a scripting language you fool.

VBScript is a scripting language (a pretty crappy one). Its not very much like VB.

Regular VB can be used for Server Side Web Page Scripting though (ASP.NET)

People who even think there's a clear line between 'scripting languages' and other languages are the true fools.

Are we allowed to call people fools on this forum?

commented: sorry you dont like my comments, it's okay though some day you will understand ;) -1
commented: No, I don't think he will ever care to understand. +6

Are you trying to make an ass out of yourself?

here try this one then. VB sucks, the language is weak and there is no supprise that VB works better as a scripting language (dur VBScript) than VB works as a software language. hope that clears things up for you ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.