F
Fabian H
Klarer Fall fuer Pointer!
Nachtrag: Hab den Code mal in den Post selber kopiert, sonst ist es recht verwirrend.
Und nachtraeglich noch einer.
Ich hoffe, man kann das "Tutorials.de"-T einigermassen gut erkennen.
Nachtrag: Hab den Code mal in den Post selber kopiert, sonst ist es recht verwirrend.
C:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define DATAFILE "presidents.txt"
#define SKIP_SPACES 1
#define BUFLEN 1024
/* Test if a given string contains particular characters.
*
* Returns 1 if the characters in <needle> occur in <haystack> or 0 if not.
* */
int contains_chars(const char *haystack, const char *needle)
{
if (needle == NULL || haystack == NULL)
return 0;
/* the comparison has to be repeated at most strlen(haystack) times */
for (; *haystack && *needle; haystack++) {
#ifdef SKIP_SPACES
/* skip whitespaces for user convenience */
while (isspace(*haystack)) haystack++;
while (isspace(*needle)) needle++;
if (!*haystack || !*needle)
break;
#endif
/* actual comparison */
if (tolower(*haystack) == tolower(*needle))
needle++;
}
/* the needle pointer has reached the end of the string if the search was
* successful */
return *needle == '\0';
}
/* helper function */
char *rtrim(char *s)
{
int l = strlen(s);
if (l) {
while (s[l - 1] <= 32)
s[--l] = '\0';
}
return s;
}
int main(int argc, char *argv[])
{
char linebuf[BUFLEN];
FILE *inp;
int found = 0;
if (argc <= 1) {
fprintf(stderr, "Usage: %s <search pattern>\n", argv[0]);
return 1;
}
inp = fopen(DATAFILE, "r");
if (inp == NULL) {
fprintf(stderr, "%s: Couldn't open data file `%s'.\n", argv[0], DATAFILE);
return 2;
}
while (fgets(linebuf, BUFLEN, inp)) {
if (contains_chars(linebuf, argv[1])) {
printf("Match: %s\n", rtrim(linebuf));
found = 1;
}
}
if (!found) {
printf("Sorry, no matches.\n");
}
if (fclose(inp) != 0) {
fprintf(stderr, "%s: WARNING: fclose() failed.\n", argv[0]);
return 2;
}
return 0;
}
Und nachtraeglich noch einer.
Ich hoffe, man kann das "Tutorials.de"-T einigermassen gut erkennen.
PHP:
#!/usr/bin/env php
<?php
($n=strtolower(@$argv[1]))||die("Usage: ".
"search.php <pattern>\n");$p=file("presidents.".
"txt");foreach($p as $j=>$h){ $h=strtolower($h);
foreach(str_split($n)as $c){if(($i=strpos($h,$c))
===false){$j=-1;
break;}$h=substr
($h,$i+1);}($j!=
-1)&&($s[]=$p[$j
]);}@array_walk(
$s,'pri'.'ntf')||
printf("Sorry, ".
"no matches\n");
Anhänge
Zuletzt bearbeitet von einem Moderator: