Funções de comparação de String
Publicado por Ivan Rocha 04/06/2007
[ Hits: 8.535 ]
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); }
Validador de Cartão de Credito
Nenhum comentário foi encontrado.
Passkeys: A Evolução da Autenticação Digital
Instalação de distro Linux em computadores, netbooks, etc, em rede com o Clonezilla
Título: Descobrindo o IP externo da VPN no Linux
Armazenando a senha de sua carteira Bitcoin de forma segura no Linux
Enviar mensagem ao usuário trabalhando com as opções do php.ini
Como configurar posicionamento e movimento de janelas no Lubuntu (Openbox) com atalhos de teclado
Máquinas Virtuais com IP estático acessando Internet no Virtualbox
Instalar o Microsoft Edge no Slackware 15
Instalando Brave Browser no Linux Mint 22
vídeo pra quem quer saber como funciona Proteção de Memória:
crontab executar em tempos (1)
Formatando cartão de memoria que nao formata[AJUDA] (9)
Programa duplicado no "Abrir com" e na barra de pesquisa do ... (3)