/* ------------------------------------------------------------------------- ** ** str.c - implementacao das funcoes para manipulacao de strings. ** ** CURSO DE PROGRAMACAO - II ** ** Prof. Márcio Santi. ** ------------------------------------------------------------------------- ** */ #include #include #include #include /* ------------------------------------------------------------------------- ** Funcoes locais: */ /* ------------------------------------------------------------------------- ** Funcoes publicas: */ /* =============================== StrPut =============================== */ void StrPut(char *s) { int i; for (i = 0; s[i] != '\0'; i++) putchar(s[i]); } /* =============================== StrGet =============================== */ int StrGet(int lim, char *s) { int i,c; int l = lim - 1; for (i = 0; i < l; i++) { c = getchar( ); if (c == EOF || c == '\n') break; s[i] = c; } s[i] = '\0'; return(i); } /* =============================== StrLen =============================== */ int StrLen(char *s) { int i; for (i = 0; s[i] != '\0'; i++); return(i); } /* =============================== StrCpy =============================== */ void StrCpy(char *s, char *t) { int i; for (i = 0; t[i] != '\0'; i++) s[i] = t[i]; s[i] = '\0'; } /* =============================== StrCat =============================== */ void StrCat(char *s, char *t) { int i,j; for (i = StrLen(s), j = 0; t[j] != '\0'; i++, j++) s[i] = t[j]; s[i] = '\0'; } /* =============================== StrCmp =============================== */ int StrCmp(char *s, char *t) { int i; /* Percorre as strings enquanto elas forem iguais. */ for (i = 0; s[i] == t[i]; i++) { /* Se as strings chegaram ao fim entao elas sao iguais. */ if (s[i] == '\0' || t[i] == '\0') return(0); } /* Retorna a diferenca entre os caracteres de cada string. */ return(s[i] - t[i]); } /* =============================== StrFnd =============================== */ int StrFnd(char *s, char *t) { int i,j,k; /* Verifica se as strings sao validas. */ if (s[0] == '\0' || t[0] == '\0') return(0); /* Percorre a string "s" ate' o seu final. */ for (i = 0; s[i] != '\0'; i++) { /* Percorre as strings "s" e "t" enquanto "t" pertencer a "s". */ for (j = 0, k = i; (t[j] != '\0') && (t[j] == s[k]); j++, k++); /* Se chegou ao fim de "t" entao toda a string pertence a "s". */ if (t[j] == '\0') return(1); } return(0); } /* =============================== StrUpp =============================== */ void StrUpp(char *s) { int i; for (i = 0; s[i] != '\0'; i++) s[i] = toupper(s[i]); s[i] = '\0'; } /* =============================== StrLow =============================== */ void StrLow(char *s) { int i; for (i = 0; s[i] != '\0'; i++) s[i] = tolower(s[i]); s[i] = '\0'; } /* =============================== StrAlc =============================== */ char *StrAlc(int n) { char *s = (char *) calloc(n, sizeof(char)); if (s == NULL) { printf("\nMemoria insuficiente na funcao StrAlc.\n"); exit(1); } return(s); } /* =============================== StrDup =============================== */ char *StrDup(char *s) { int i,n; char *t; /* Aloca memoria para a nova string (inclui o '\0'). */ n = StrLen(s) + 1; t = StrAlc(n); /* Copia os caracteres (inclusive o '\0'). */ for (i = 0; i < n; i++) t[i] = s[i]; return(t); }