Informatiker Board (http://www.informatikerboard.de/board/index.php)
- Themengebiete (http://www.informatikerboard.de/board/board.php?boardid=1)
--- Theoretische Informatik (http://www.informatikerboard.de/board/board.php?boardid=5)
---- formale Sprachen (http://www.informatikerboard.de/board/board.php?boardid=12)
----- strstr(wort1, wort2) - wort1 in C (http://www.informatikerboard.de/board/thread.php?threadid=3500)


Geschrieben von Verwirrt am 16.03.2017 um 12:57:

  strstr(wort1, wort2) - wort1 in C

Meine Frage:
Hallo,

ich muss in C ein Programm schreiben welches mir in einem String den Startindex des ersten Vorkommens eines anderen String zurück gebe.
Ich hab es geschafft nur verstehe ich eine Sache nicht ganz genau:

Wenn ich zum Beispiel das wort hallo nehme und als zweiten String ll nehme bekomme ich bei strstr(wort1, wort2) - wort1 = 2 raus, was der Startindex ist. Ich verstehe nicht ganz warum dort 2 raus kommt, da eigentlich die Rechnung doch so aussieht:

strstr(wort1, wort2) = llo und dann macht man ja so gesehen llo - hallo.

Deswegen warum kommt bei ll - hallo = 2 raus.

Meine Ideen:
Einzige Idee die ich habe ist, dass C mir anzeigt wie viele Buchstaben mehr hallo als llo hat.
Stimmt das?



Geschrieben von eulerscheZahl am 22.03.2017 um 06:48:

 

strstr gibt dir nicht "llo" zurück, sondern einen Verweis, wo im Speicher "llo" steht.
Und das ist 2 Zeichen nach dem Wortanfang. Wenn du mit "- wort1" einen Pointer abziehst, ist die Differenz der Pointer 2.


Forensoftware: Burning Board, entwickelt von WoltLab GmbH