Pular para o conteúdo

Retirar ocorrências de uma substring dentro de uma string

Este script demonstra uma função que retira as ocorrências de uma substring dentro de uma string.
Danilo Renato da Silva danilo_renato
Hits: 11.103 Categoria: C/C++ Subcategoria: Miscelânea
  • Download
  • Nova versão
  • Indicar
  • Denunciar

Descrição

Este script demonstra uma função que retira as ocorrências de uma substring dentro de uma string.
Download retira_ocorrencias.cpp Enviar nova versão

Esconder código-fonte

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* retira_ocorrencias(char* str, char* substr)
{
   int i = 0; int j = 0; int f = 0;
   int tam = strlen(str);
   int tamsub = strlen(substr);
   char *ret = (char*) malloc(tam);
   
   while(i < tam)
   {
      while(str[i] == substr[j])
      {
         i++;
         j++;   
         
         if(j >= tamsub)
         {            
            break;
         }   
      } 

      if(j < tamsub)
      {
         i = i - j;         
      }
   
      ret[f] = str[i];      
      i++; f++;
      j = 0;

   }   

   ret[f] = '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* retira_ocorrencias(char* str, char* substr)
{
   int i = 0; int j = 0; int f = 0;
   int tam = strlen(str);
   int tamsub = strlen(substr);
   char *ret = (char*) malloc(tam);
   
   while(i < tam)
   {
      while(str[i] == substr[j])
      {
         i++;
         j++;   
         
         if(j >= tamsub)
         {            
            break;
         }   
      } 

      if(j < tamsub)
      {
         i = i - j;         
      }
   
      ret[f] = str[i];      
      i++; f++;
      j = 0;

   }   

   ret[f] = '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* retira_ocorrencias(char* str, char* substr)
{
   int i = 0; int j = 0; int f = 0;
   int tam = strlen(str);
   int tamsub = strlen(substr);
   char *ret = (char*) malloc(tam);
   
   while(i < tam)
   {
      while(str[i] == substr[j])
      {
         i++;
         j++;   
         
         if(j >= tamsub)
         {            
            break;
         }   
      } 

      if(j < tamsub)
      {
         i = i - j;         
      }
   
      ret[f] = str[i];      
      i++; f++;
      j = 0;

   }   

   ret[f] = '{FONTE}';

   return ret;

}

int main()
{
   char* str = (char*) malloc(100); 
   char* sub = (char*) malloc(100);

   printf("Entre com a string:    ");    gets(str);
   printf("Entre com a substring: ");    gets(sub);

   char* resultado = retira_ocorrencias(str, sub);
   printf("%s\n",resultado);
   return 0;
}
';    return ret; } int main() {    char* str = (char*) malloc(100);    char* sub = (char*) malloc(100);    printf("Entre com a string:    ");    gets(str);    printf("Entre com a substring: ");    gets(sub);    char* resultado = retira_ocorrencias(str, sub);    printf("%s\n",resultado);    return 0; }
';    return ret; } int main() {    char* str = (char*) malloc(100);    char* sub = (char*) malloc(100);    printf("Entre com a string:    ");    gets(str);    printf("Entre com a substring: ");    gets(sub);    char* resultado = retira_ocorrencias(str, sub);    printf("%s\n",resultado);    return 0; }

Manipulando argumentos com getopt_long

Validador de CPF

Squid - Modo de Trabalho

Jogo Tetris em C - parte 2

inputAst

#1 Comentário enviado por danilo_renato em 31/05/2012 - 13:30h
Onde está ret[f] = '{FONTE'} leia-se: ret[f] = '\{0}' (sem as chaves)

Contribuir com comentário

Entre na sua conta para comentar.