#include <stdio.h> #include <string.h> int endWith(char* stack, char* needle) { char* posStack = stack; char* posNeedle = needle; while (*stack) ++stack; while (*needle) ++needle; while (stack != posStack && needle != posNeedle) { if (*stack != *needle) return 0; --stack; --needle; } return needle == posNeedle; } int main(void) { /* 1 */ char* a = "Hello World!"; char* b = strstr(a, "World!"); int lengthword = strlen("World!"); int length = 0; if (b == NULL) printf("ne se termine pas par.\n"); length = strlen(b); if (lengthword == length) printf("se termine bien par.\n"); else printf("ne se termine pas par.\n"); /* 2 */ if (endWith("Hello World!", "World!")) printf("se termine bien par.\n"); else printf("ne se termine pas par.\n"); return 0; }
#include <stdio.h> #include <string.h> int SeTerminePar(char *a, char *b) { unsigned long lmotif, lsrc; int v; lmotif = strlen(b); lsrc = strlen(a); if(lmotif > lsrc) return 0; lsrc -= lmotif; v = strcmp(a + lsrc, b); return ((v == 0) ? 1: 0); } int main() { char *a = "Hello World!"; char *b = strstr(a, "Coucou"); printf("se termine par=%d\n", SeTerminePar(a,b)); }
#include <stdio.h> #include <string.h> int endWith(char* stack, char* needle) { char* posStack = stack; char* posNeedle = needle; while (*stack) ++stack; while (*needle) ++needle; while (stack != posStack && needle != posNeedle) { if (*stack != *needle) return 0; --stack; --needle; } return needle == posNeedle; } int main(void) { /* 1 */ char* a = "Hello World! coucou"; char* b = strstr(a, "World!"); int lengthword = strlen("World!"); int length = 0; if (b == NULL) printf("ne se termine pas par.\n"); length = strlen(b); if (lengthword == length) printf("se termine bien par.\n"); else printf("ne se termine pas par.\n"); /* 2 */ if (endWith(a, b)) printf("se termine bien par.\n"); else printf("ne se termine pas par.\n"); return 0; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPar contre si le mot est contenu mais pas à la fin, le deuxième test ne fonctionne pas.