Advertisement
iconic14

Eliminazione sottostringa da testo libero

Jun 22nd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int Controllo (int, char *, char*);
  6. void Taglia (int, int, char*);
  7.  
  8. int main () {
  9.     int i, j, continua = 1;
  10.     char c, stringa[40], sotto[40];
  11.    
  12.     printf("Inserisci testo libero (\n per terminare) > ");
  13.     gets(stringa);
  14.     printf("Inserisci token da eliminare > ");
  15.     gets(sotto);
  16.    
  17.     stringa[strlen(stringa)] = '\0';
  18.    
  19.     while(continua){
  20.     for (i=0; i<strlen(stringa); i++)
  21.     {
  22.         continua = 0;
  23.        
  24.         if (stringa[i] == sotto[0]){
  25.             int elimina = Controllo(i, stringa, sotto);
  26.            
  27.             if (elimina){
  28.                 continua = 1;
  29.                 Taglia(i, strlen(sotto), stringa);
  30.                 i --;
  31.             }
  32.     }
  33.     }
  34. }
  35.     printf("%s", stringa);
  36. }
  37. int Controllo (int pos, char* stringa, char* sotto)
  38. {
  39.     int analisi;
  40.     int j;
  41.     analisi = strlen(sotto);
  42.    
  43.     for (j=0; j<analisi; j++)
  44.     {  
  45.         if (stringa[pos + j] != sotto[j])
  46.            return 0;
  47.     }  
  48.    
  49.     return 1;
  50. }
  51.  
  52. void Taglia(int pos, int len, char *stringa){
  53.    
  54.     int j;
  55.    
  56.     for (j=0; j<(strlen(stringa) - pos); j++){
  57.         stringa[j + pos] = stringa[j + pos + len];
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement