Funções de comparação de String
Publicado por Ivan Rocha 04/06/2007
[ Hits: 8.534 ]
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); }
Exemplo de gabaritos de função
Parte 6 - Sessão de estudo sobre VETORES
primeirafuncao.c - Criando uma função
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
Instalando Brave Browser no Linux Mint 22
vídeo pra quem quer saber como funciona Proteção de Memória:
Encontre seus arquivos facilmente com o Drill
Mouse Logitech MX Ergo Advanced Wireless Trackball no Linux
Compartilhamento de Rede com samba em modo Público/Anônimo de forma simples, rápido e fácil
Como abrir o pycharm no linux [RESOLVIDO] (4)
VMs e Interfaces de Rede desapareceram (12)