jazykc  2013.3
upoljazykc
 All Classes Files Functions Variables Typedefs Macros Pages
porovnani-textovych-retezcu.c
Go to the documentation of this file.
1 
31 #include <stdio.h>
32 #include <stdlib.h>
33 
34 #include <string.h>//strlen(), jinak impliciticni deklarace => na 64bitech Segmentation fault!!!
35 
36 int up1_strcmp (const char *s1, const char *s2) {
37  while (*s1 && *s2 && *s1 == *s2) {
38  s1++, s2++;
39  }
40  return *s1 - *s2;//musime normalizovat na -1,0,1 ? strcmp() to taky nevraci
41 }
42 
43 char *up1_strchr (const char *s, int c) {
44  while (*s && *s++ != c)
45  ;//zadne telo
46  return *s == c ? (char*)s : NULL;
47 }
48 
49 char *up1_strrchr (const char *s, int c) {
50  return NULL; //TODO
51 }
52 
53 size_t up1_strlen (const char *s) {
54  size_t len = 0;
55  while (*s++) len++;
56  return len;
57 }
58 
59 #define TEST_STRLEN(s) \
60  printf("strlen(\"%s\")=%d dtto up1_strlen(\"%s\")=%d\n", (s), strlen((s)), (s), up1_strlen((s)))
61 
62 int main (int argc, char *argv[]) {
63  int diff;
64  if (argc < 3) {
65  fprintf(stderr, "Usage: %s slovo1 slovo2\n", argv[0]); return EXIT_FAILURE;
66  }
67  diff = up1_strcmp(argv[1], argv[2]);
68  printf("Slovo \"%s\" je %s slovo \"%s\"\n",
69  argv[1], diff ? diff > 0 ? "vetsi nez" : "mensi nez" : "rovno", argv[2]);
70 
71  printf("strlen(\"%s\")=%d dtto up1_strlen(\"%s\")=%d\n", argv[1], strlen(argv[1]), argv[1], up1_strlen(argv[1]));
72  TEST_STRLEN(argv[0]);
73  TEST_STRLEN(argv[2]);
74  TEST_STRLEN("");
75  return EXIT_SUCCESS;
76 }