C-Kurs/PalindromeMusterlösung: Unterschied zwischen den Versionen
< C-Kurs
(Die Seite wurde neu angelegt: „<pre> #include <stdio.h> char* reverse(char wort[]) { char* result = calloc(strlen(wort) + 1, sizeof(char)); int i; for(i = 1; i <= strlen(wort); i++) *(res...“) |
|||
Zeile 19: | Zeile 19: | ||
if (argc < 2) return -1; | if (argc < 2) return -1; | ||
printf("%s\n%s\n", argv[1], reverse(argv[1])); | printf("%s\n%s\n", argv[1], reverse(argv[1])); | ||
− | + | printf("%d\n", isReverse(argv[1], argv[1])); | |
+ | } | ||
</pre> | </pre> |
Version vom 24. September 2009, 09:45 Uhr
#include <stdio.h> char* reverse(char wort[]) { char* result = calloc(strlen(wort) + 1, sizeof(char)); int i; for(i = 1; i <= strlen(wort); i++) *(result++) = wort[strlen(wort) - i]; *result = '\0'; return result - strlen(wort); } int isReverse(char* wort1, char* wort2) { char* wort1Rev = reverse(wort1); return strcmp(wort1Rev, wort2); } int main(int argc, char* argv[]) { if (argc < 2) return -1; printf("%s\n%s\n", argv[1], reverse(argv[1])); printf("%d\n", isReverse(argv[1], argv[1])); }