Este código fonte, irá ajudar, quando editar uma string de texto, na linguagem C.
| Arquivo: carol.string.c |
| #define rBuffer 2000 int rCharArrayMax = 25; char rCharUpper[][10] = {"Á", "É", "Í", "Ó", "Ú", "Ã", "?", "I", "Õ", "U", "Â", "Ê", "Î", "Ô", "Û", "À", "Ü", "Ç", "Ñ", "C", "G", "H", "J", "S", "U"}; char rCharLower[][10] = {"á", "é", "í", "ó", "ú", "ã", "?", "i", "õ", "u", "â", "ê", "î", "ô", "û", "à", "ü", "ç", "ñ", "c", "g", "h", "j", "s", "u"}; char rCodeUpper[][10] = {"\230", "\231", "\232", "\233", "\234", "\235", "\236", "\237", "\240", "\241", "\242", "\243", "\244", "\245", "\246", "\247", "\250", "\251", "\252", "\253", "\254", "\255", "\256", "\257", "\260"}; char rCodeLower[][10] = {"\261", "\262", "\263", "\264", "\265", "\266", "\267", "\300", "\301", "\302", "\303", "\304", "\305", "\306", "\307", "\310", "\311", "\312", "\313", "\314", "\315", "\316", "\317", "\320", "\321"}; #define rResult 0 #define null "\0" #define NUL '\0' #define NUM_ELEM(x) (sizeof(x)/sizeof( *(x))) typedef enum {true = 0, false = 1} boolean; /* ************************* String ************************* */ // rReplace(): O metodo rReplace voce insere uma palavra ou uma letra em que voce deseja substituir por outra. char *rReplace(char *st, char *orig, char *repl) { static char __memo1[rBuffer]; static char __memo2[rBuffer]; static char __memo3[rBuffer]; strcpy(__memo1, st); strcpy(__memo2, orig); strcpy(__memo3, repl); static char buffer[rBuffer]; char *ch; if (!(ch = strstr(st, orig))) return st; strncpy(buffer, st, ch-st); buffer[ch-st] = 0; sprintf(buffer+(ch-st), "%s%s", repl, ch+strlen(orig)); strcpy(st, __memo1); strcpy(orig, __memo2); strcpy(repl, __memo3); return buffer; } // __rInvert(): X char *__rInvert(char *st1){ int i,j; static char st2[rBuffer]; for(i=0;st1[i];i++); i--; for(j=0,i;i>=0;j++){ st2[j]=st1[i]; i--; } st2[j]='\0'; return st2; } // __rIndexOf(): X int __rIndexOf(char *source,char *code){ char static __memo1[rBuffer]; strcpy(__memo1, ""); strcpy(__memo1, source); char static __memo2[rBuffer]; strcpy(__memo2, ""); strcpy(__memo2, code); char *found = strstr(source, code); int index = -1; if (found != NUL){ index = found - source; } strcpy(source, ""); strcpy(source, __memo1); strcpy(code, ""); strcpy(code, __memo2); return index; } // __rCharCode(): X char *__rCharCode(char *texto){ static char s1[10]; static char s2[10]; int x = 0; int s = 0; do{ do { strcpy(s1, rCharLower[x]); strcpy(s2, rCodeLower[x]); strcpy(texto, rReplace(texto, s1, s2)); s = __rIndexOf(texto, s1); if(s < 0){ x++; } else if(s == 0){ break; } }while(true); }while(x > rCharArrayMax); x = 0; s = 0; do{ do { strcpy(s1, rCharUpper[x]); strcpy(s2, rCodeUpper[x]); strcpy(texto, rReplace(texto, s1, s2)); s = __rIndexOf(texto, s1); if(s < 0){ x++; } else if(s == 0){ break; } }while(true); }while(x > rCharArrayMax); return texto; } // __rLastIndexOf(): X int __rLastIndexOf(char *source, char *code){ static char __memo1[rBuffer]; strcpy(__memo1, ""); strcpy(__memo1, source); static char __memo2[rBuffer]; strcpy(__memo2, ""); strcpy(__memo2, code); static char xsource[rBuffer]; strcpy(xsource, __rInvert(source)); int n = strlen(source); int index = __rIndexOf(xsource, code); strcpy(source, ""); strcpy(source, __memo1); strcpy(code, ""); strcpy(code, __memo2); return n - index; } // rLength(): Retorna o comprimento de uma String. int rLength(char *texto){ return strlen(texto); } // rSubstring(): O metodo rSubstring (subcadeia) retorna a cadeia que se estende do indice Inicial ate o caractere logo antes de indice final. Se indice Inicial for maior que indice final, o metodo funciona como se ambos estivessem com as posicoes trocadas. Se os dois indices forem iguais, retorna uma cadeia vasia. char *__rSubstring(char *origem, int inicio, int quant){ static char __memo1[rBuffer]; strcpy(__memo1, origem); char *res = origem; int i = 0; if((inicio < 0) || (inicio > strlen(origem))) inicio = 0; if(quant > inicio + strlen(origem)) quant = strlen(origem) - inicio; for(i = 0; i <= quant - 1; i++){ res[i] = origem[inicio + i]; } res[i] = '\0'; return res; } char *rSubstring(char *origem, int inicio, int quant){ static char __memo1[rBuffer]; static char __s[rBuffer]; strcpy(__memo1, origem); __rSubstring(origem, inicio, quant); strcpy(__s, origem); strcpy(origem, __memo1); return __s; } // rCharAt(): Funciona da mesma forma que o rSubstring, so que, retorna o caractere simples em uma posicao especÃfica na String. char rCharAt(char *texto,int x){ x = x - 1; char s = texto[x]; return s; } // rIndexOf(): O metodo rIndexOf faz a busca no objeto string pela primeira ocorrencia do caractere e retorna o indice correspondente. o argumento inicIndex é opcional e indica a partir de onde inicia a busca. Podemos localizar os valores dos Ãndices para todos os caracteres do mesmo tipo iniciando apos o Ãndice anterior. int rIndexOf(char *source,char *code, int xIndex){ static char __memo1[rBuffer]; static char __memo2[rBuffer]; strcpy(__memo1, source); strcpy(__memo2, code); strcpy(source, __rCharCode(source)); strcpy(code, __rCharCode(code)); static char xSource[rBuffer]; static char charSource[rBuffer]; int x = 0; strcpy(charSource, ""); for(x=0; x<xIndex; x++){ strcat(charSource, "\222"); } int n1 = strlen(source); strcpy(xSource, source); strcpy(xSource, rSubstring(xSource, xIndex, n1)); strcat(charSource, xSource); strcpy(source, __memo1); strcpy(code, __memo2); return __rIndexOf(charSource, code); } // rLastIndexOf(): O metodo rLasIndexOf é identico ao metodo index of exceto que faz a busca do caractere na cadeia do fim para o comeco iniciando em inicIndex. int rLastIndexOf(char *source, char *code, int xIndex){ static char __memo1[rBuffer]; static char __memo2[rBuffer]; strcpy(__memo1, source); strcpy(__memo2, code); strcpy(source, __rCharCode(source)); strcpy(code, __rCharCode(code)); static char xSource[rBuffer]; static char charSource[rBuffer]; static char xCode[rBuffer]; int n1 = strlen(source); int x = 0; strcpy(charSource, ""); for(x=xIndex; x<n1; x++){ strcat(charSource, "\222"); } strcpy(xSource, source); strcpy(xSource, rSubstring(xSource, 0, xIndex)); strcat(xSource, charSource); strcpy(charSource, __rInvert(xSource)); strcpy(xCode, ""); strcpy(xCode, __rInvert(code)); int index = __rIndexOf(charSource, xCode); index = (n1 - index) - 1; if(index>xIndex){ return -1; } strcpy(source, __memo1); strcpy(code, __memo2); return index; } int _rIndexOf(char *source,char *code){ int x = rIndexOf(source, code, 0); return x; } int _rLastIndexOf(char *source, char *code){ int f = rLength(source); int x = rLastIndexOf(source, code, f); return x; } // rToLowerCase(): O metodo rToLowerCase (para minusculo) retorna a cadeia com todos os caracteres alterados para minusculo. char *rToLowerCase(char *stg){ static char __memo1[rBuffer]; strcpy(__memo1, stg); static char texto[rBuffer]; static char s1[10]; static char s2[10]; int x; for(x=0;x<strlen(stg);x++){ texto[x] = tolower(stg[x]); } x = 0; int s = 0; do{ do { strcpy(s1, rCharUpper[x]); strcpy(s2, rCharLower[x]); strcpy(texto, rReplace(texto, s1, s2)); s = __rIndexOf(texto, s1); if(s < 0){ x++; } else if(s == 0){ break; } }while(true); }while(x > rCharArrayMax); strcpy(stg, __memo1); return texto; } // rToUpperCase(): O metodo rToUpperCase (para maiusculo) retorna a cadeia com todos os caracteres alterados para maiusculo. char *rToUpperCase(char *stg){ static char __memo1[rBuffer]; strcpy(__memo1, stg); static char texto[rBuffer]; static char s1[10]; static char s2[10]; int x; for(x=0;x<strlen(stg);x++){ texto[x] = toupper(stg[x]); } x = 0; int s = 0; do{ do { strcpy(s1, rCharLower[x]); strcpy(s2, rCharUpper[x]); strcpy(texto, rReplace(texto, s1, s2)); s = __rIndexOf(texto, s1); if(s < 0){ x++; } else if(s == 0){ break; } }while(true); }while(x > rCharArrayMax); strcpy(stg, __memo1); return texto; } // rCompareTo(): X int rCompareTo(char *string1, char *string2){ return strcmp(string1, string2); } // rCompareToIgnoreCase(): X int rCompareToIgnoreCase(char *string1, char *string2){ static char st1[rBuffer]; static char st2[rBuffer]; strcpy(st1, rToLowerCase(string1)); strcpy(st2, rToLowerCase(string2)); return strcmp(st1, st2); } // rEquals(): X boolean rEquals(char *string1, char *string2){ boolean sn = true; if(strcmp(string1, string2) == 0){ sn = true; } else { sn = false; } return sn; } // rEqualsIgnoreCase(): X boolean rEqualsIgnoreCase(char *string1,char *string2){ static char st1[rBuffer]; static char st2[rBuffer]; boolean sn = true; strcpy(st1, rToLowerCase(string1)); strcpy(st2, rToLowerCase(string2)); if(strcmp(st1, st2) == 0){ sn = true; } else { sn = false; } return sn; } |
| Um Exemplo para Teste: string.c |
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include "carol.string.c" int main(int argc, char **argv){ // rLength(): Retorna o comprimento de uma String. char s01[] = "Esta é uma string de texto"; printf("\nEsta é uma string de texto\n"); printf("rLength() = %d\n", rLength(s01)); // rSubstring(): O comando substring (subcadeia) retorna a cadeia que se estende do indice Inicial até o caractere logo antes de indice final. Se indice Inicial for maior que indice final, o método funciona como se ambos estivessem com as posições trocadas. Se os dois índices forem iguais, retorna uma cadeia vasia. char s02[] = "Esta é uma string de texto"; printf("\nEsta é uma string de texto\n"); printf("rSubstring(4, 6) = |%s", rSubstring(s02, 4, 6)); printf("|\n"); char pp[] = "Olá Mundo!!!"; printf("\nOlá Mundo!!!\n"); printf("rSubstring(2, 9) = %s\n", rSubstring(pp, 2, 9)); printf("%s\n", pp); // rCharAt(): Funciona da mesma forma que o substring, só que, retorna o caractere simples em uma posição específica na String. char s03[] = "Esta é uma string de texto"; printf("\nEsta é uma string de texto\n"); printf("charAt(6) = %c\n", rCharAt(s03, 6)); // rReplace(): O comando rReplace você insere uma palavra ou uma letra em que você deseja substituir por outra. char s04[] = "Este automovel é caro"; char at[] = "automovel"; char bt[] = "carro"; printf("\nEste automovel é caro\n"); printf("rReplace() = %s\n", rReplace(s04, at, bt)); // rToLowerCase(): O comando rToLowerCase retorna a cadeia com todos os caracteres alterados para minúsculo. char s05[] = "ISTO É UM Ñ É Ó Ç EXEMPLO"; printf("\nISTO É UM Ñ É Ó Ç EXEMPLO\n"); char xx[rBuffer]; printf("rToLowerCase() = %s\n", rToLowerCase(s05)); // rToUpperCase(): O comando rToUpperCase retorna a cadeia com todos os caracteres alterados para maiúsculo. char s06[] = "isto é um ñ ó ç exemplo"; printf("\nisto é um ñ ó ç exemplo\n"); printf("rToUpperCase() = %s\n", rToUpperCase(s06)); // rCompareTo(): X char Texto1[] = "A"; char Texto2[] = "B"; printf("\nTexto1 = \"A\", Texto2 = \"B\"\n"); printf("rCompareTo() = %d\n", rCompareTo(Texto1, Texto2)); char mTexto1[] = "A"; char mTexto2[] = "A"; printf("\nTexto1 = \"A\", Texto2 = \"A\"\n"); printf("rCompareTo() = %d\n", rCompareTo(mTexto1, mTexto2)); char fTexto1[] = "B"; char fTexto2[] = "A"; printf("\nTexto1 = \"B\", Texto2 = \"A\"\n"); printf("rCompareTo() = %d\n", rCompareTo(fTexto1, fTexto2)); // rCompareToIgnoreCase(): X char w01[] = "hello world"; char w02[] = "HELLO WORLD"; printf("\na = \"hello world\"\nb = \"HELLO WORLD\"\n"); printf("rCompareToIgnoreCase() = %d\n", rCompareToIgnoreCase(w01, w02)); // rEquals(): X char wa[] = "A"; char wb[] = "a"; printf("\na = \"A\"\nb = \"a\"\n"); printf("rEquals() = %d\n", rEquals(wa, wb)); // rEqualsIgnoreCase(): X char za[] = "A"; char zb[] = "a"; printf("\na = \"A\"\nb = \"a\"\n"); printf("rEqualsIgnoreCase() = %d\n", rEqualsIgnoreCase(za, zb)); printf("\n=======================\n"); printf("rCharUpper[] = "); int i = 0; for(i = 0; i<rCharArrayMax; i++){ printf("%s, ", rCharUpper[i]); } printf("\n"); printf("rCharLower[] = "); i = 0; for(i = 0; i<rCharArrayMax; i++){ printf("%s, ", rCharLower[i]); } printf("\n"); printf("\n=======================\n"); printf("rCodeUpper[] = "); i = 0; for(i = 0; i<rCharArrayMax; i++){ printf("%s, ", rCodeUpper[i]); } printf("\n"); printf("rCodeLower[] = "); i = 0; for(i = 0; i<rCharArrayMax; i++){ printf("%s, ", rCodeLower[i]); } printf("\n"); printf("\n=======================\n"); printf("Hello world!\n"); char texto00[] = "Hello world!"; char tt00x[] = "l"; char tt00y[] = "d"; printf("_rIndexOf((\"l\") = %d\n", _rIndexOf(texto00, tt00x)); printf("_rLastIndexOf(\"d\") = %d\n", _rLastIndexOf(texto00, tt00y)); printf("=======================\n"); printf("Hello world!\n"); printf("while rIndexOf(\"l\") = "); char text02[] = "Hello world!"; int f = rLength(text02); int index = 0; char tt02[] = "l"; int n = rLastIndexOf(text02, tt02, f); while (index < n){ index = rIndexOf(text02, tt02, index + 1); printf("%d, ", index); } printf("\n"); // indexOf(): O comando indexOf faz a busca no objeto string pela primeira ocorrência do caractere e retorna o índice correspondente. o argumento inicIndex é opcional e indica a partir de onde inicia a busca. Podemos localizar os valores dos Ãndices para todos os caracteres do mesmo tipo iniciando após o Ãndice anterior. // rLastIndexOf(): O método rLasIndexOf é idêntico ao método index of exceto que faz a busca do caractere na cadeia do fim para o começo iniciando em inicIndex. char text01[] = "Hello world!"; char tt01[] = "d"; strcpy(text02, "Hello world!"); char tt03[] = "d"; printf("\nHello world!\n"); printf("rIndexOf(\"d\") = %d\n", rIndexOf(text01, tt01, 0)); printf("rLastIndexOf(\"d\") = %d\n", rLastIndexOf(text02, tt03, 12)); index = 0; strcpy(tt02, "l"); n = rLastIndexOf(text02, tt02, 0); printf("\nHello world!\n"); printf("rIndexOf(\"l\") = %d\n", rIndexOf(text02, tt02, 0)); printf("rLastIndexOf(\"l\") = %d\n", rLastIndexOf(text02, tt02, 12)); /* while (index < n){ index = rIndexOf(text02, tt02, index + 1); printf("%d\t", index); } printf("|\n"); */ char s[] = "HeÇÇo WorÇd!"; char tt[] = "Ç"; printf("\n\nHeÇÇo WorÇd!\n"); printf("rLastIndexOf(\"Ç\", 12) = %d\n", rLastIndexOf(s, tt, 12)); printf("rLastIndexOf(\"Ç\", 9) = %d\n", rLastIndexOf(s, tt, 9)); printf("rLastIndexOf(\"Ç\", 3) = %d\n", rLastIndexOf(s, tt, 3)); printf("rLastIndexOf(\"Ç\", 2) = %d\n", rLastIndexOf(s, tt, 2)); printf("\n"); printf("rIndexOf(\"Ç\", 0) = %d\n", rIndexOf(s, tt, 0)); printf("rIndexOf(\"Ç\", 3) = %d\n", rIndexOf(s, tt, 3)); printf("rIndexOf(\"Ç\", 4) = %d\n", rIndexOf(s, tt, 4)); printf("rIndexOf(\"Ç\", 10) = %d\n", rIndexOf(s, tt, 10)); // printf("rIndexOf(\"Ç\", 11) = %d\n", rIndexOf(s2, tt, 11)); return 0; } |