Funções de comparação de String
Publicado por Ivan Rocha 04/06/2007
[ Hits: 8.720 ]
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);
}
Meu primeiro programa em C (Olá mundo)
Exemplo de um "Jogo de Labirinto"
Estrutura condicional ternária
Nenhum comentário foi encontrado.
Modo Simples de Baixar e Usar o bash-completion
Monitorando o Preço do Bitcoin ou sua Cripto Favorita em Tempo Real com um Widget Flutuante
Instalando partes faltantes do Plasma 6
Adicionar botão "mostrar área de trabalho" no Zorin OS
Como montar um servidor de backup no linux
Estou tentando ser legalista, mas tá complicado! (9)
espelhar monitores nao funciona (2)
SQLITE não quer funcionar no LINUX LMDE6 64 com Lazaruz 4.2 64bit (n... (1)









