Ponteiros
Publicado por Enzo de Brito Ferber 15/09/2006
[ Hits: 5.038 ]
Homepage: http://www.maximasonorizacao.com.br
Operações com strings com alto uso de ponteiros. De quebrar uma função tipo a strncmp. :-) Altamente recomendado para quem está aprendendo ponteiros.
/*
* Programa: Operações com strings
* Arquivo: strings.c
* Autor: Enzo Ferber 'Slackware_10'
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int check(char *str, char *part){
int i;
char *aux;
while(*str){
aux = str;
*str++;
if(!(cmp(aux, part,strlen(part)))){
i=1;
break;
}else
i=0;
}
return i;
}
int cmp(char *str1, char *str2, int len){
int i = len;
int res;
for(i=0;i<len;i++){
if(*(str1+i) == *(str2+i)) continue;
else return 1;
}
return 0;
}
char *makeLower(char *str){
int i;
for(i=0;i<strlen(str);i++) str[i] = tolower(str[i]);
return str;
}
int main(void){
char *str1;
char *str2;
char op;
str1 = (char *)malloc(50*sizeof(char));
str2 = (char *)malloc(50*sizeof(char));
if(!str1 || !str2){
printf("- Erro ao alocar memoria\n");
exit(1);
}
printf("String1: "); __fpurge(stdin); scanf("%[^\n]", str1);
printf("String2: "); __fpurge(stdin); scanf("%[^\n]", str2);
__fpurge(stdin);
printf("Diferenciar Maiusculas/Minusculas [s/n]: "); scanf("%c", &op);
switch(op){
case 'n':
str1 = makeLower(str1);
str2 = makeLower(str2);
break;
case 's':
break;
}
switch(check(str1, str2)){
case 1:
printf("String1 contem String2\n");
break;
case 0:
printf("String1 nao contem String2\n");
break;
}
free(str1);
free(str2);
return 0;
}
Nenhum comentário foi encontrado.
LazyDocker – Interface de Usuário em Tempo Real para o Docker
Instalando COSMIC no Linux Mint
Turbinando o Linux Mint: o poder das Nemo Actions
Inteligência Artificial no desenvolvimento de software: quando começar a usar?
O widget do Plasma 6 Área de Notificação
[Resolvido] Algo deu errado ao abrir seu perfil
Quando vocês pararam de testar distros? (14)
Problema com som no laptop (3)
Não estou conseguindo fazer funcionar meu Postfix na versão 2.4 no Deb... (2)









