Funções de comparação de String
Publicado por Ivan Rocha 04/06/2007
[ Hits: 8.766 ]
Homepage: http://homes.dcc.ufba.br/~ivan062/bahia
Alguns exemplos de funções de comparação de strings e execução delas, incluindo também a conhecida strcmp.
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
int comparaStrings1(char str1[], char str2[], int qtd){
int i = 0;
while(i < qtd){
if((str1[i] == '{FONTE}') && (str2[i] == '{FONTE}'))
return(0);
else{
if (toupper(str1[i]) == toupper(str2[i]))
i += 1;
else{
if(toupper(str1[i]) > toupper(str2[i]))
return(1);
if(toupper(str1[i]) < toupper(str2[i]))
return(-1);
}
}
}
}
int comparaStrings2(char *s, char *t){
for(; toupper(*s) == toupper(*t) ; s++,t++)
if(toupper(*s) =='{FONTE}')
return(0);
return(*s - *t);
}
int comparaStrings3(char str1[], char str2[]){
int i = 0;
for(; toupper(str1[i]) == toupper(str2[i]) ; i++)
if((str1[i]) =='{FONTE}')
return(0);
return((str1[i]) - (str2[i]));
}
int main(){
char str1[20], str2[20], str3[20], str4[20];
system("clear");
printf("Programa que compara Strings!!!\n");
printf("\nDigite a primeira string: ");
gets(str1);
printf("\nDigite a segunda string: ");
gets(str2);
printf("\nDigite a terceira string: ");
scanf("%s", &str3);
getchar();
printf("\nDigite a quarta string: ");
scanf("%s", &str4);
printf("\nFUNCAO 1: \n"); //************************************
printf("\nComparacao gets com gets: \n\n"); //*********************
if(comparaStrings1(str1, str2, 20) < 0)
printf("%s eh menor que %s\n", str1, str2);
if(comparaStrings1(str1, str2, 20) > 0)
printf("%s eh maior que %s\n", str1, str2);
if(comparaStrings1(str1, str2, 20) == 0)
printf("%s eh igual a %s\n", str1, str2);
printf("\nComparacao gets com scanf: \n\n"); //*******************
if(comparaStrings1(str1, str3, 20) < 0)
printf("%s eh menor que %s\n", str1, str3);
if(comparaStrings1(str1, str3, 20) > 0)
printf("%s eh maior que %s\n", str1, str3);
if(comparaStrings1(str1, str3, 20) == 0)
printf("%s eh igual a %s\n", str1, str3);
printf("\nComparacao scanf com gets: \n\n"); //*******************
if(comparaStrings1(str4, str2, 20) < 0)
printf("%s eh menor que %s\n", str4, str2);
if(comparaStrings1(str4, str2, 20) > 0)
printf("%s eh maior que %s\n", str4, str2);
if(comparaStrings1(str4, str2, 20) == 0)
printf("%s eh igual a %s\n", str1, str3);
printf("\nComparacao scanf com scanf: \n\n"); //******************
if(comparaStrings1(str3, str4, 20) < 0)
printf("%s eh menor que %s\n", str3, str4);
if(comparaStrings1(str3, str4, 20) > 0)
printf("%s eh maior que %s\n", str3, str4);
if(comparaStrings1(str3, str4, 20) == 0)
printf("%s eh igual a %s\n", str3, str4);
printf("\nFUNCAO 2: \n"); //*************************************
printf("\nComparacao gets com gets: \n\n"); //*********************
if(comparaStrings2(str1, str2) < 0)
printf("%s eh menor que %s\n", str1, str2);
if(comparaStrings2(str1, str2) > 0)
printf("%s eh maior que %s\n", str1, str2);
if(comparaStrings2(str1, str2) == 0)
printf("%s eh igual a %s\n", str1, str2);
printf("\nComparacao gets com scanf: \n\n"); //*******************
if(comparaStrings2(str1, str3) < 0)
printf("%s eh menor que %s\n", str1, str3);
if(comparaStrings2(str1, str3) > 0)
printf("%s eh maior que %s\n", str1, str3);
if(comparaStrings2(str1, str3) == 0)
printf("%s eh igual a %s\n", str1, str3);
printf("\nComparacao scanf com gets: \n\n"); //*******************
if(comparaStrings2(str4, str2) < 0)
printf("%s eh menor que %s\n", str4, str2);
if(comparaStrings2(str4, str2) > 0)
printf("%s eh maior que %s\n", str4, str2);
if(comparaStrings2(str4, str2) == 0)
printf("%s eh igual a %s\n", str1, str3);
printf("\nComparacao scanf com scanf: \n\n"); //******************
if(comparaStrings2(str3, str4) < 0)
printf("%s eh menor que %s\n", str3, str4);
if(comparaStrings2(str3, str4) > 0)
printf("%s eh maior que %s\n", str3, str4);
if(comparaStrings2(str3, str4) == 0)
printf("%s eh igual a %s\n", str3, str4);
printf("\nFUNCAO 3: \n"); //*************************************
printf("\nComparacao gets com gets: \n\n"); //*********************
if(comparaStrings3(str1, str2) < 0)
printf("%s eh menor que %s\n", str1, str2);
if(comparaStrings3(str1, str2) > 0)
printf("%s eh maior que %s\n", str1, str2);
if(comparaStrings3(str1, str2) == 0)
printf("%s eh igual a %s\n", str1, str2);
printf("\nComparacao gets com scanf: \n\n"); //*******************
if(comparaStrings3(str1, str3) < 0)
printf("%s eh menor que %s\n", str1, str3);
if(comparaStrings3(str1, str3) > 0)
printf("%s eh maior que %s\n", str1, str3);
if(comparaStrings3(str1, str3) == 0)
printf("%s eh igual a %s\n", str1, str3);
printf("\nComparacao scanf com gets: \n\n"); //*******************
if(comparaStrings3(str4, str2) < 0)
printf("%s eh menor que %s\n", str4, str2);
if(comparaStrings3(str4, str2) > 0)
printf("%s eh maior que %s\n", str4, str2);
if(comparaStrings3(str4, str2) == 0)
printf("%s eh igual a %s\n", str1, str3);
printf("\nComparacao scanf com scanf: \n\n"); //******************
if(comparaStrings3(str3, str4) < 0)
printf("%s eh menor que %s\n", str3, str4);
if(comparaStrings3(str3, str4) > 0)
printf("%s eh maior que %s\n", str3, str4);
if(comparaStrings3(str3, str4) == 0)
printf("%s eh igual a %s\n", str3, str4);
printf("\nFUNCAO 4: \n"); //*************************************
printf("\nComparacao gets com gets: \n\n"); //*********************
if(strcmp(str1, str2) < 0)
printf("%s eh menor que %s\n", str1, str2);
if(strcmp(str1, str2) > 0)
printf("%s eh maior que %s\n", str1, str2);
if(strcmp(str1, str2) == 0)
printf("%s eh igual a %s\n", str1, str2);
printf("\nComparacao gets com scanf: \n\n"); //*******************
if(strcmp(str1, str3) < 0)
printf("%s eh menor que %s\n", str1, str3);
if(strcmp(str1, str3) > 0)
printf("%s eh maior que %s\n", str1, str3);
if(strcmp(str1, str3) == 0)
printf("%s eh igual a %s", str1, str3);
printf("\nComparacao scanf com gets: \n\n"); //*******************
if(strcmp(str4, str2) < 0)
printf("%s eh menor que %s\n", str4, str2);
if(strcmp(str4, str2) > 0)
printf("%s eh maior que %s\n", str4, str2);
if(strcmp(str4, str2) == 0)
printf("%s eh igual a %s", str1, str3);
printf("\nComparacao scanf com scanf: \n\n"); //******************
if(strcmp(str3, str4) < 0)
printf("%s eh menor que %s\n", str3, str4);
if(strcmp(str3, str4) > 0)
printf("%s eh maior que %s\n", str3, str4);
if(strcmp(str3, str4) == 0)
printf("%s eh igual a %s\n\n", str3, str4);
return (0);
}
Resolução de uma fórmula de mátemática
Nenhum comentário foi encontrado.
KDE Plasma - porque pode ser a melhor opção de interface gráfica
Gentoo: detectando impressoras de rede e como fixar uma impressora por IP
Como o GNOME conseguiu o feito de ser preterido por outras interfaces gráficas
Por que sua empresa precisa de uma PKI (e como automatizar EMISSÕES de certificados via Web API)
Instalando NoMachine no Gentoo com Systemd (acesso Remoto em LAN)
Gentoo: Trocando wpa_supplicant pelo iwd no NetworkManager (Systemd)
Instalar Linux em notebook Sony Vaio VPCEG13EB (10)
Vou destruir sua infância:) (6)
Quando vocês pararam de testar distros? (24)









