Advertisement
Emanuele_Bruno

Esame 9

Dec 6th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. string desi2(string x); // restituisce le ultime due lettere di una stringa
  6. string stringab(string a[],int b);  // restituisce b="" se non ci sono corrispondenze
  7.                                 // oppure i primi 4 car. di 3 string di uguale desinenza
  8.  
  9. int main()
  10. {
  11.     string a[]={"ilmnzz","abcd56","123456","efgh56"};
  12.     cout << "Provo a costruire la stringa 'b' partendo da un array 'a' di 4 stringhe : " << stringab(a,4);
  13.     return 0;
  14. }
  15.  
  16. string desi2(string x)
  17. {
  18.     string temp;
  19.     temp=x.substr(x.length()-2,2);
  20.     return temp;
  21. }
  22.  
  23. string stringab(string a[],int c)
  24. {
  25.     int i=0;
  26.     string b;
  27.     while (i<c)
  28.     {
  29.         b=a[i].substr(0,4);
  30.         int j=0;
  31.         int check=0;
  32.         while (j<c)
  33.         {
  34.             if (i==j)
  35.             {
  36.                  if ((j+1)!=c) j++;
  37.                  else
  38.                  {
  39.                     b="";
  40.                     return b;
  41.                  }
  42.             }
  43.             if (desi2(a[i])==desi2(a[j]))
  44.             {
  45.                 b+=a[j].substr(0,4);
  46.                 check++;
  47.                 if (check==2) return b;
  48.             }
  49.             j++;
  50.         }
  51.         i++;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement