
-----------------------------------
jenkl
Tue Feb 17, 2004 9:19 pm

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.

-----------------------------------
jonos
Tue Feb 17, 2004 9:31 pm


-----------------------------------
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.

-----------------------------------
Tony
Tue Feb 17, 2004 10:38 pm


-----------------------------------
eh... just use index() function. It returns the position of the substring within the string.

-----------------------------------
jonos
Wed Feb 18, 2004 7:23 am


-----------------------------------
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.

-----------------------------------
Paul
Wed Feb 18, 2004 12:37 pm


-----------------------------------
It could if you ran a for loop, and checking ever letter with a counter:

 
var word: string:="Bad Bad"
var counter: string :=0
for a: 1..length(word)
if index ("a", word(a)) not = 0 then
counter+=1
end if
end for
put "there are ", counter, " a's." 

-----------------------------------
naoki
Wed Feb 18, 2004 5:17 pm


-----------------------------------
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

-----------------------------------
jenkl
Wed Feb 18, 2004 6:06 pm


-----------------------------------


index ("a", word(a))

 

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.

-----------------------------------
Paul
Wed Feb 18, 2004 6:58 pm


-----------------------------------
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...

-----------------------------------
jenkl
Wed Feb 18, 2004 8:25 pm


-----------------------------------
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.

-----------------------------------
jonos
Thu Feb 19, 2004 8:09 am


-----------------------------------
mine way up there is easy enough, it doesn't use index.

-----------------------------------
TheZsterBunny
Thu Feb 19, 2004 8:26 am


-----------------------------------
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

var message := "thAt is bAd Apples"

and A is your search string

you could do this

message := message(index(message,"A")..*)
and put in a loop until index(message,"A") = 0

I'll work on it more later, if you need.

-z0rQ

-----------------------------------
Paul
Fri Feb 20, 2004 11:18 am


-----------------------------------

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:

var sent: string:="aaaaAAAAaaAAAAAA"
%usually if you want to look for A, you would use
%the "A" as the pattern, but if you want to replace
%all the A's then use it as string and use every single letter
%in the string as the pattern, and compare it against A.
for z: 1..length(sent)
if index ("A",sent(a)) not =0 then
sent:=sent(1..z-1)+"a"+sent(z+1..*)%replaces all A's with a's
end if
end for
put sent

You could do the same with any letter, or is that not the question?

-----------------------------------
jenkl
Sat Feb 21, 2004 9:32 am


-----------------------------------
alright. thanks. i understood it yesterday. thakns a bunch everyone.

oh ya, you can find that contest i was talking about at http://contest-cemc.uwaterloo.ca/ccc/past/docs/2002_Computing_Contest_(E).pdf

I figured it out, thanks again.
