I have a question. How can I be able to compare one string to another. An example being that I have Str0 equal, let's say, "HI" and I have the user input something in Str1. I want to see if Str0=Str1, and if so, do a command. I originally though Str0=Str1, but that gave an error. I thought of inString(Str1,Str0), but that gave an error as well. Does anyone have an answer?
Sure, but one thing that stinks about TI-Basic is that you have to compare the lengths of the stings before you compare them, and that will destroy two variables, Ans, and some other for this one: (NOTE: 68k Basic can do this.... Razz)


Code:
length(Str1)=length(Str0
If Ans
prod(seq(A=inString(Str0,sub(Str1,A,1)),A,1,length(Str1


Oh, and Ans will contain the result. For example, do this directly after:

Code:
If Ans:Then
; True, strings are same
Else
; False, Different
End
OK, so I soft of understand how to do this. Is there by chance an easier way, as I will be repeating this about 8 or 9 times.
Uh, if you use inString, it does work.

Code:
"HI"->Str1
Repeat inString(Str1,Str2)
Input "",Str2
End

Or at least it worked on my calc.
I got that to work to, the only problem is that it isn't something that is typed in and can be repeated, it is a one, time thing.

Here is a snippet of the code for better understanding:

Code:
length(Str0)->A
inString(Str0," ")->B
If B>0
Then
sub(Str0,1,B-1)->Str2
sub(Str0,B+1,length(Str0)-B)->Str0
End
If B=0
Str0->Str2


The two sub parts if for dividing a string with spaces into two different parts. Example: "Hello There" in Str1 to "Hello" in Str1 and "There" in Str2. It can go up to ten words, but when I run the code like this, which continues for a few sets, the other strings have the same thing, so it would be "Hello" in Str1 and "There" in Str2-Str9
Try this for what you told me:

Code:
" "->Str1
While 1
Input "",Str2
If inString(Str1,Str2)
Then
Disp "You already entered this!"
Else
Str1+Str2->Str1
End
End
MrDew25 wrote:
The two sub parts if for dividing a string with spaces into two different parts. Example: "Hello There" in Str1 to "Hello" in Str1 and "There" in Str2. It can go up to ten words, but when I run the code like this, which continues for a few sets, the other strings have the same thing, so it would be "Hello" in Str1 and "There" in Str2-Str9


That is because the way your code works is that it doesn't stop if there are no more spaces to parse. Smile Here's how you could do something that would take up to 10 words in Str0 and put them in Str1-Str0 respectively: (NOTE: If there is less than 10 strings, the other string variables are left alone.

Hope this helps! Smile

EDIT: This can also be changed around to prevent ever having to store the strings anywhere, so theoretically you could have a super-long string. Applications include a text editor's file names....

EDIT5: Oh wait, is this for some kind of command-line interface? If so, remember this: Ans is your best friend. Very Happy

(Assuming the input Str0 is something like: "Hello There")


Code:
" "+Str0+" ->Str0:1->A
For(X,0,9
A+1->B:inString(Str0," ",Ans->A
If Ans:Then
sub(Str0,B,A-B
If not(X:Ans->Str1
If X=1:Ans->Str2
If X=2:Ans->Str3
If X=3:Ans->Str4
If X=4:Ans->Str5
If X=5:Ans->Str6
If X=6:Ans->Str7
If X=7:Ans->Str8
If X=8:Ans->Str9
If X=9:Ans->Str0
Else:9->X
End:End

EDIT: Also, if you want, you could do a quick modification like this to tell you how many words there are: (Output in C variable)

Code:
DelVar C" "+Str0+" ->Str0:1->A
For(X,0,9
A+1->B:inString(Str0," ",Ans->A
If Ans:Then
sub(Str0,B,A-B
If not(X:Ans->Str1
If X=1:Ans->Str2
If X=2:Ans->Str3
If X=3:Ans->Str4
If X=4:Ans->Str5
If X=5:Ans->Str6
If X=6:Ans->Str7
If X=7:Ans->Str8
If X=8:Ans->Str9
If X=9:Ans->Str0
C+1->C
Else:9->X
End:End

Hope this helps! Smile It sure beats having to check each string. Smile

EDIT2: Sorry if my above code was kind of confusing; let me expalin it a little better.


Code:
length(Str1)=length(Str0

This makes sure that the strings are the same length. If they aren't, they obviously don't match. Razz

Code:
If Ans

This executes the next statement if the lengths match

Code:
prod(seq(A=inString(Str0,sub(Str1,A,1)),A,1,length(Str1

This is just some silly stuff that compares each character to the other string.

Code:
If Ans:Then

This executes if the strings are the same. If the strings are not the same length, this is skipped as well, because the previous statement wasn't executed.

Code:
length(Str1)=length(Str0
If Ans
prod(seq(A=inString(Str0,sub(Str1,A,1)),A,1,length(Str1
If Ans:Then
; True, strings are same
Else
; False, Different
End
Mateo, your code is great, I tested it multiple times and it works every time, except there is one thing. When entering a phrase less than ten words, Str0 is the original phrase with an added space in the beginning. The code that I used was:
MateoConLechuga wrote:

Code:
" "+Str0+" ->Str0:1->A
For(X,0,9
A+1->B:inString(Str0," ",Ans->A
If Ans:Then
sub(Str0,B,A-B
If not(X:Ans->Str1
If X=1:Ans->Str2
If X=2:Ans->Str3
If X=3:Ans->Str4
If X=4:Ans->Str5
If X=5:Ans->Str6
If X=6:Ans->Str7
If X=7:Ans->Str8
If X=8:Ans->Str9
If X=9:Ans->Str0
Else:9->X
End:End


Edit: I fixed it. After the to Ends, I placed in a small line of code

Code:
If A=0:""->Str0

This seemed to fix the problem.

Edit 2: Since I am this far, I will say that I am making a hangman game. I have made it so that it will only take the first five words, with Str6-Str0 being the dashes where the missing letters go. I need help placing the letters back into the strings. Would you have any knowledge on how to do that?

Code:
" "+Str0+" ->Str0

So this line adds a space at the beginning and end of the original string in order to make indexing simpler. If you want Str0 to be the first word, and Str9 to be the last word, you could do something like this: Smile


Code:
" "+Str0+" ->Str9:1->A
For(X,0,9
A+1->B:inString(Str9," ",Ans->A
If Ans:Then
sub(Str0,B,A-B
If not(X:Ans->Str0
If X=1:Ans->Str1
If X=2:Ans->Str2
If X=3:Ans->Str3
If X=4:Ans->Str4
If X=5:Ans->Str5
If X=6:Ans->Str6
If X=7:Ans->Str7
If X=8:Ans->Str8
If X=9:Ans->Str9
Else:9->X
End:End
Yes, I have fixed that so it doesn't do that anymore. If you have read my second edit, do you know of any way if could do that?
MrDew25 wrote:
Since I am this far, I will say that I am making a hangman game. I have made it so that it will only take the first five words, with Str6-Str0 being the dashes where the missing letters go. I need help placing the letters back into the strings. Would you have any knowledge on how to do that?

Sorry, I am a tad confused... Razz So you have strings for the dashes? Could you make it so the dashes are simply drawn depending on the length of the inputted strings? Ah, and am I correct in thinking that you are trying to have a letter inputted, and then to place the letter in the correct spot? One bit of advice I have is that inString() returns the location of the specified string, and can also be used as the third argument. Smile
It doesn't seem like you are, but I will explain it nonetheless. The game I am making is Hangman. I have Str1-Str5 dedicated to holding the words and I have Str6-Str0 dedicated to holding the dashes (Str1="Hello", Str6="-----", Str2="Hi", Str7="--", etc.). I was wondering if there was a way, when someone inputted a number by pressing the corresponding button, to place a letter inside of the string without getting rid of the other dashes/letters all ready in there.
The way that you are trying to do the dashes is a little more confusing than it needs to be. All you need to do is to use length(Str#) to find the total length of the string, and then use a for loop to display dashes that many times.
Here's a sample of how that would work.

Code:
"HELLO"->Str1
ClrHome
For(A,1,length(Str1))
Output(1,A,"-")
End

One more thing you could try to reduce the number of strings that you use is to just separate each group by 10 spaces and have a work inside that group. That way you can use instring to display every 10 characters so that you would only use 1 string. I don't know if that's better, but it makes the program seem a little cleaner Razz.
No, I have that part all taken care of, I have it as:

Code:
length(Str1)->A
"-"->Str6
For(B,1,A)
"-"+Str6->Str6
End

What I was wondering if there was a way to input something into a string without getting rid of whatever is in the string.
Example:
Str1="xxxxx"
I want to make it so I can insert something, let's say 3, into the string. I want it be be "xx3xx".
x=Either a "-" or a letter.
Changing a single part of the string is a lot harder. You need to copy all values until the string and then the part afterwards, while skipping the center value, but overall, you don't need to do that. Here's a sample code for how to get the parts to display.

Code:
ClrHome
"HELLOTHERE"->Str0
"1000100110"->Str1
For(A,1,length(Str0))
If sub(Str1,A,1)="1"
Then
Output(A,1,sub(Str0,A,1))
Else
Output(A,1,"-")
End
Pause
Great idea. Let's say, then, that the user presses the button for L. How could the Str1 change to "1011100110" without it being pre-written? Could I do something like (after using lists):

Code:
For(A,1,length(Str1)
inString(Str1,Str2,A-1)->B
1->{L1}(B)
End
You can have it in a list and change the value as things get stored into the list. I haven't tested it, so this might be the wrong syntax.



Code:
ClrHome
"HELLOTHERE"->Str0
{1,0,0,1,1,0,0,0,1,1}->L1
1->L1(2)
For(A,1,length(Str0))
If L1(A)
Then
Output(A,1,sub(Str0,A,1))
Else
Output(A,1,"-")
End
Pause
So, I am putting the dashes into strings and using them that way. Could this work?

Code:
length(Str1)->dim({L1})
{L1}->{L6}
For(A,1,length(Str1)
inString(Str1,Str2,A-1)->B
1->{L1}(B)
If {L1}(A)=1
Then
Str6+sub(Str1,A,1)->Str6
Else
"-"+Str6->Str6

Edit: This {L1}->{L6} is for checking later on to see if they guessed a letter correctly
Uh MrDew, don't think that everything needs to be in a string in order to display it. Really, there only needs to be 1 string, and it can be the whole size of RAM. This way, it makes a much shorter code because you can just use for loops for different string parts. In the code I put before, I used output in order to show what I wanted to display and did not store any dashes. Really, you don't need to store any dashes, just when you need to display them, do so with a simple function like the one I posted above.
So, instead of storing them in strings, I use the output method that you did in the If statement. Besides that though, how does the code look?

Thank you for everyone that helped. I have completed the program and hope to get it uploaded later tonight. I will post this thread's link in the description so that you guy scan get credit for helping.

NOTE: For anyone that came here from my hangman program overview, anyone that helped before this post helped with the project. Anyone that helped after this post did not.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement