jazykc  2013.3
upoljazykc
 All Classes Files Functions Variables Typedefs Macros Pages
hledani-podretezce-zprava.c
Go to the documentation of this file.
1 
25 #include <stdio.h>
26 #include <stdlib.h>
27 
28 static size_t up1_strlen (const char *s) {
29  size_t len = 0;
30  while (*s++) len++;
31  return len;
32 }
33 
34 //extern char *strstr();
35 
36 char *strrstr (const char *kupka, const char *jehla) {
37  char *pch2, *pk, *pj;
38  if (!*jehla) return kupka;//nebo jehla?
39  pch2 = kupka + up1_strlen(kupka) - up1_strlen(jehla);
40 
41  while (pch2 >= kupka) {
42  pk = pch2, pj = jehla;
43  while (*pj && *pj++ == *pk++)
44  ;
45  if (!*pj) return pch2;//jehla na konci
46  pch2--;
47  }
48  return NULL;
49 }
50 
51 #define STRRSTR(haystack, needle)\
52  printf("Text: \"%s\"\nHledame: \"%s\"\nVraceny ukazatel: \"%s\"\n", (haystack),(needle),strrstr((haystack),(needle)))
53 
54 int main(int argc, char *argv[]) {
55  STRRSTR("Ahoj svete!", "svet");
56  STRRSTR("Ahoj svete v jedne vete!", "vete");
57  STRRSTR("kupka zadna jehla", "");//co vracet?
58  STRRSTR("", "jehla zadna kupka");
59  return EXIT_SUCCESS;
60 }