Skip to content

Instantly share code, notes, and snippets.

@ichistmeinname
Created November 27, 2013 09:07
Show Gist options
  • Save ichistmeinname/7672793 to your computer and use it in GitHub Desktop.
Save ichistmeinname/7672793 to your computer and use it in GitHub Desktop.
######################################################
# Algorithmus aus der Vorlesung
######################################################
#text= "Die Giraffe trinkt Kaffee.";
#sub = "affe";
#i=0;
#while i < text.length && text[i,sub.length] != sub do
# i = i + 1;
#end;
#puts("'" + sub + "' kommt in '" + text +
# "' " + if i == text.length
# then "nicht"
# else ""
# end + " vor.");
#########################################################
# Umwandlung in Prozedur und statische Strings weglassen
########################################################
#def subStringSearch(text, sub)
#
# i=100;
# while i < text.length && text[i,sub.length] != sub do
# i = i + 1;
# end;
# puts("'" + sub + "' kommt in '" + text +
# "' " + if i >= text.length
# then "nicht"
# else ""
# end + " vor.");
#end;
#########################################################
# Stelle des Vorkommens von sub in text ausgeben
########################################################
#def subStringSearch(text, sub)
#
# i=0;
# while i < text.length && text[i,sub.length] != sub do
# i = i + 1;
# end;
# puts("'" + sub + "' kommt in '" + text +
# "' " + if i >= text.length
# then "nicht"
# else "an Position " + i.to_s
# end + " vor.");
#end;
#########################################################
# Finden eines Substringes auslagern
########################################################
# Funktion gibt die Position zurück, an der `sub` in `text` vorkommt
# und sonst -1 (-1 als "Fehlerfall")
#def findSub(text, sub)
# i=0;
# while i < text.length && text[i,sub.length] != sub do
# i = i + 1;
# end;
# if i >= text.length then
# i = -1;
# end;
#
# return i;
#end;
#
#def subStringSearch(text, sub)
# while
# pos = findSub(text, sub);
# puts("'" + sub + "' kommt in '" + text +
# "' " + if pos == -1
# then "nicht"
# else "an Position " + pos.to_s
# end + " vor.");
#end;
#
#########################################################
# Übergebe Startposition bei der Suche nach `sub`
########################################################
# Funktion gibt die Position zurück, an der `sub` in `text` vorkommt
# und sonst -1 (-1 als "Fehlerfall")
#def findSub(startPos, text, sub)
# i = startPos;
# while i < text.length && text[i,sub.length] != sub do
# i = i + 1;
# end;
# if i >= text.length then
# i = -1;
# end;
#
# return i;
#end;
#
#def subStringSearch(text, sub)
# pos = findSub(0, text, sub);
#
# if pos == -1 then
# puts( "'" + sub + "'" + " kommt in " + "'" + text + "'" + "nicht vor.");
# else
# while pos > -1 do
# puts("'" + sub + "' kommt in '" + text +
# "' " + "an Position " + pos.to_s + " vor.");
# pos = findSub(pos + 1, text, sub);
# end;
# end;
#
#end;
#########################################################
# Unterstreiche `sub` in `text`
########################################################
# Funktion gibt die Position zurück, an der `sub` in `text` vorkommt
# und sonst -1 (-1 als "Fehlerfall")
def findSub(startPos, text, sub)
i = startPos;
while i < text.length && text[i,sub.length] != sub do
i = i + 1;
end;
if i >= text.length then
i = -1;
end;
return i;
end;
def underline(startPos, length, text)
puts(text);
# sub unterstreichen
puts((" " * startPos) + ("*" * length));
end;
def subStringSearch(text, sub)
pos = findSub(0, text, sub);
if pos == -1 then
puts( "'" + sub + "'" + " kommt in " + "'" + text + "'" + "nicht vor.");
else
while pos > -1 do
underline(pos,sub.length, text);
pos = findSub(pos + 1, text, sub);
end;
end;
end;
text = "Die Giraffe trinkt Kaffee.";
sub = "affe";
subStringSearch(text, sub);
subStringSearch(text, "tee");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment