Page 1 of 1

project help

Posted: Wed Nov 02, 2005 11:41 am
by dneisner
I am in my final semester of community college. I have to take a q-basic programming class and I am struggling very badly. I have approached the professor and he is so intelligent but he just can't seem to put it into language that I understand. Our project is to write a program that prints a first name - last name output regardless of the format it was inputted in. Ex: Input John Smith prints John Smith Input Smith, John prints John Smith. I wrote a synapsis of the problem as follows: Seperate a given string into substrings and print the first and last substring names in seperate columns. Now I am floundering. First, Is Mid$ the best function to use to seperate the substrings? Second How do I make it recognize the space or the comma as the seperators between strings? and Last How do I tell the program how to chose which substring to print first? I have read our book (Q-Basic using modular structure) and it is not very helpful. The teacher does not use it at all because he says it is worthless and I have tried google searches. I'm not asking for you to do the work, just to point me in the right direction. Thanks!! Deb

Posted: Wed Nov 02, 2005 11:58 am
by MystikShadows
Hi Deb,

Basically, one thing I can tell you is that the INSTR$ function is your friend here. It is defined as:

Code: Select all

INSTR$([Start,] Text, SearchFor)
INSTR$ returns the position where SearchFor was found in Text. It should be what you need to determine if there's a comma or a space.

Technically, the user should enter John Smith, Smith,John or maybe even Smith, John (with a space and a comma). so the first part of this should be looking for which one of the three it is.

Code: Select all

SpaceResult% = INSTR$(NameString$, " ")
CommaResult% = INSTR$(NameString$, ",")

IF SpaceResult% > 0 THEN
   IF CommaSpaceResult% > 0 THEN
       ' this is lastname, firstName
   ELSE
       ' this is FirstName LastName, just print it as is
   END IF
ELSE
   ' LastName,FirstName (no space)
END IF
To filter out the first and last name when they are lastname, first name you'll just need to do it with left$ and right$

hope this helps :-)

project help

Posted: Wed Nov 02, 2005 1:21 pm
by dneisner
Mystik, Thank you very much that was very helpful and wonder of wonders I am relatively sure that I understood your explanation. Do you want to teach the rest of my class? Oh well it was worth a shot. I do have one question for you though. When I use INSTR$ to search for the space how would I handle it if it found more than one occurence, such as if the person used a middle name or middle initial? I don't want to print them. Thanks again. Deb

Posted: Wed Nov 02, 2005 1:39 pm
by MystikShadows
Let's assume the following:

Name1String$ = "Garcello, Marcel R."
Name2String$ = "Leana R. Jackson"

Hence one needs to be inverted and the other does not.

As I mentionned INSTR$'s first parameter is where to start the search from in the string. So, you'd need 2 Position variable, let's call them:

Code: Select all

FirstSpace1% = INSTR$(Name1String$, " ")
SecondPlace1% = INSTR$(FirstSpace1% + 1, Name1String$, " ")
FirstSpace2% = INSTR$(Name2String$, " ")
SecondPlace2% = INSTR$(FirstSpace2% + 1, Name2String$, " ")
In the case of name1String$:
FirstSpace1% should be 9
SecondSpace1% should be 17

In the case of name2String$:
FirstSpace2% should be 5
SecondSpace2% should be 8

Now since Name1String$ should be inverted since the last name is first you just need to get the following.

Code: Select all

' Beware of linewraps in this window..should be 2 lines only, not 3 that you might see.
LastName$ = LEFT$(Name1String$, FirstSpace1% - 1)
' FirstName is what's between your two found positions, + 2 for the ", "
FirstName$ = MID$(Name1String$, FirstSpace1% + 2, SecondSpace1%-FirstSpace1% + 1)
In the case of Name2String$ which doesn't need to be inverted you just get everything before FirstSpace2% and everything after SecondSpace2% like so:

Code: Select all

' Beware of linewraps in this window..should be 2 lines only, not 3 that you might see.
FirstName$ = LEFT$(Name2String$, FirstSpace2% - 1)
LastName$ = RIGHT$(Name2String$, LEN(Name2String$) - SecondSpace2% + 1)
Then you just print those 2 variables:

Code: Select all

PRINT FirstName$; " "; LastName$
And there you have it. I can't quite teach you the rest of the course since I don't have the teacher's program at hand :-). But if you have more questions, do feel free to ask :-).

Hope this helps

project reply

Posted: Wed Nov 02, 2005 3:00 pm
by dneisner
:) Mystik Thanks again. After some reading and re-reading I think I can put them together to get the basic parts of the program I want to write. As long as I make the effort and show that I understand what it needs to do and which functions and such I would use, even if it has bugs he'll accept it for at least half credit. I only have 5 weeks left and all I want is a passing grade to graduate. I will be back here before this is over with many more questions I'm sure. Thanks again for all the help. Deb