FunÁűes de comparaÁ„o de String

Publicado por Ivan Rocha 04/06/2007

[ Hits: 8.256 ]

Homepage: http://homes.dcc.ufba.br/~ivan062/bahia

Download Stringcompara




Alguns exemplos de funções de comparação de strings e execução delas, incluindo também a conhecida strcmp.

  



Esconder cůdigo-fonte

# 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);
}

Scripts recomendados

Aloca String

DATA POR EXTENSO

Jogo do Labirinto no Terminal

Convers„o do nķmero de dias em anos (meu segundo programa em C)

Mťdia do aluno


  

ComentŠrios

Nenhum comentŠrio foi encontrado.


Contribuir com comentŠrio




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts