Checking for a pattern or specific character in a string...
Author |
Message |
jenkl
|
Posted: 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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
jonos
|
Posted: Tue Feb 17, 2004 9:31 pm Post subject: (No 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. |
|
|
|
|
|
Tony
|
Posted: Tue Feb 17, 2004 10:38 pm Post subject: (No subject) |
|
|
eh... just use index() function. It returns the position of the substring within the string. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
jonos
|
Posted: Wed Feb 18, 2004 7:23 am Post subject: (No 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. |
|
|
|
|
|
Paul
|
Posted: Wed Feb 18, 2004 12:37 pm Post subject: (No subject) |
|
|
It could if you ran a for loop, and checking ever letter with a counter:
code: |
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
|
Posted: Wed Feb 18, 2004 5:17 pm Post subject: (No 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 |
|
|
|
|
|
jenkl
|
Posted: Wed Feb 18, 2004 6:06 pm Post subject: (No subject) |
|
|
code: |
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
|
Posted: Wed Feb 18, 2004 6:58 pm Post subject: (No 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... |
|
|
|
|
|
Sponsor Sponsor
|
|
|
jenkl
|
Posted: Wed Feb 18, 2004 8:25 pm Post subject: (No 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. |
|
|
|
|
|
jonos
|
Posted: Thu Feb 19, 2004 8:09 am Post subject: (No subject) |
|
|
mine way up there is easy enough, it doesn't use index. |
|
|
|
|
|
TheZsterBunny
|
Posted: Thu Feb 19, 2004 8:26 am Post subject: (No 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
code: | var message := "thAt is bAd Apples" |
and A is your search string
you could do this
code: | 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
|
Posted: Fri Feb 20, 2004 11:18 am Post subject: (No 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:
code: |
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
|
Posted: Sat Feb 21, 2004 9:32 am Post subject: (No 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. |
|
|
|
|
|
|
|