Computer Science Canada Checking for a pattern or specific character in a string... |
Author: | jenkl [ Tue Feb 17, 2004 9:19 pm ] |
Post subject: | Checking for a pattern or specific character in a string... |
Hello. Been reading around a lot, but this would be my first post. Anywas, I was wondering how I would take a string, and look for a specific character or couple of characters in it. |
Author: | jonos [ Tue Feb 17, 2004 9:31 pm ] |
Post subject: | |
you could use index, you could use something like var sentence : string := "i like dogs" for counter : 1..length(sentence) if sentence(counter) = "l" then put "you found an l" end if end for that will work, the code just may not be perfect. |
Author: | Tony [ Tue Feb 17, 2004 10:38 pm ] |
Post subject: | |
eh... just use index() function. It returns the position of the substring within the string. |
Author: | jonos [ Wed Feb 18, 2004 7:23 am ] |
Post subject: | |
but won't it return only one number if there are two of the same letters. doesn't it return 2 if it finds bAd bAng. im not too familiar with it. |
Author: | Paul [ Wed Feb 18, 2004 12:37 pm ] | ||
Post subject: | |||
It could if you ran a for loop, and checking ever letter with a counter:
|
Author: | naoki [ Wed Feb 18, 2004 5:17 pm ] |
Post subject: | |
or if you made counters for each position of the letter you found so if the first time you found the letter you stored the position in a, then next index search would be index (whateverletteryouwant, word (a .. *)) and then mark down the next value. mebbe using an array to store the found numbers |
Author: | jenkl [ Wed Feb 18, 2004 6:06 pm ] | ||
Post subject: | |||
let me just see if i get that line. index is the function. "a", the one in qoutes is declaring what to look for, and word(a)) is saying look at the string word's "a"th character, right? a would be used in conjunction with your for loop. |
Author: | Paul [ Wed Feb 18, 2004 6:58 pm ] |
Post subject: | |
no, in the index, index ("put string here", "put pattern here") in this case its reversed, because we use a for loop to speparate and check every letter in the word to find a specific letter "a", so in this case, "a" becomes the string, and word(a), meaning each letter, is the pattern. I think that made sense... |
Author: | jenkl [ Wed Feb 18, 2004 8:25 pm ] |
Post subject: | |
nah. i dont really get it, sorry. im not too good with anything that has to do with strings. im more of a... bad at programming kind of person myself, lol. anyways. any.. "normal" non reversed practical example you can show me. i asked this question mainly cause i was looking at 2002's CCC... the changing the spelling one. anyways, thanks for all your help. |
Author: | jonos [ Thu Feb 19, 2004 8:09 am ] |
Post subject: | |
mine way up there is easy enough, it doesn't use index. |
Author: | TheZsterBunny [ Thu Feb 19, 2004 8:26 am ] | ||||
Post subject: | |||||
Hrm, I'm short on time now, but you could get the string you are searching, index it as shown above, and then say this
and A is your search string you could do this
and put in a loop until index(message,"A") = 0 I'll work on it more later, if you need. -z0rQ |
Author: | Paul [ Fri Feb 20, 2004 11:18 am ] | ||
Post subject: | |||
jenkl wrote: i asked this question mainly cause i was looking at 2002's CCC... the changing the spelling one. anyways, thanks for all your help. What? is this one of the easy questions? anywhere I could see the questions, cause here at my school, I haven't even heard of CCC. anyway changing the letters... say, you want to change all the capital A's to small a's in a sentence heres what you do:
You could do the same with any letter, or is that not the question? |
Author: | jenkl [ Sat Feb 21, 2004 9:32 am ] |
Post subject: | |
alright. thanks. i understood it yesterday. thakns a bunch everyone. oh ya, you can find that contest i was talking about at [url]http://contest-cemc.uwaterloo.ca/ccc/past/docs/2002_Computing_Contest_(E).pdf[/url] I figured it out, thanks again. |