Advertisement
Emanuele_Bruno

Esercizio 1 del 14/07/2015

Dec 14th, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const unsigned int N=4;
  6.  
  7. void puntatore (string A[],unsigned int N, int h);
  8. bool cerca(string a, string temp);
  9.  
  10. int main()
  11. {
  12.     int h=2; // h e' il numero di caratteri da cercare
  13.     string A[]={"questos","eunesempios","distringheos","gringos!!"};
  14.     cout<<"Cerchiamo una sottostringa ripetuta di "<<h<<" caratteri... se la troviamo allora il risultato e' un cout\n";
  15.     cout<<"che punta alla locazione di memoria... ";
  16.     puntatore(A,N,h);
  17.     return 0;
  18. }
  19.  
  20. bool cerca(string a, string temp)
  21. {
  22.     unsigned int i=0, b=temp.length();
  23.     while (i<=a.length()-b)
  24.     {   //cout<<a.substr(i,b)<<"=="<<temp<<"?\n";//per debug
  25.         if (a.substr(i,b)==temp) return true;
  26.         else i++;
  27.     }
  28.     return false;
  29. }
  30.  
  31. void puntatore (string A[],unsigned int N, int h)
  32. {
  33.     unsigned int k=0,i=1;
  34.     string temp;
  35.     while (k<=A[0].length()-h)
  36.     {
  37.         temp=A[0].substr(k,h);
  38.         if (!cerca(A[i],temp))
  39.         {
  40.             k++;
  41.             i=1;
  42.         }
  43.         else i++;
  44.         if (i==N)
  45.         {
  46.             cout<<&A[0]+k;
  47.             break;//serve per uscire dal while senza errori di memoria
  48.         }
  49.     }
  50.     cout<<"Spiacente...nessuna stringa di "<<h<<" caratteri trovata..";
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement