0

> 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. :)

0

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"

0

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

0

>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 );
  }
}
0

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;
}
0

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).

Edited by happygeek: fixed formatting

1

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).

Votes + Comments
"incomplete and unclear" which is why you don't let a woman on your software development team!
0

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

0

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.....

0

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!

0

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?

0

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.

0

>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...

0

I built a perfectly fine:

database system for a local charity
word processor
network chat client

using VB

0

>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.

0

>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?

0

>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 :)

0

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...

0

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.

Votes + Comments
Just correct the misspellings: SOMTHING should be SOMETHING and AMMOUNT should be AMOUNT.
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.
0

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?

0

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)

0

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?

Votes + Comments
No, I don't think he will ever care to understand.
sorry you dont like my comments, it's okay though some day you will understand ;)
0

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 ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.