Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. std::string RequestString(int length, std::string nome)
  7. {
  8.     std::string result;
  9.     while (result.length() <= length)
  10.     {
  11.         std::cout << "Inserisci un testo "<< nome <<" di lunghezza superiore a " << length << "\n";
  12.         std::cin >> result;
  13.     }
  14.     return result;
  15. }
  16.  
  17. int main()
  18. {
  19.     // stringhe
  20.     std::string a = RequestString(3, "A");
  21.     std::string b = RequestString(3, "B");
  22.     std::string* longString;
  23.     std::string* shortString;
  24.    
  25.    
  26.     // controlli lunghezze
  27.     if (a.length() == b.length())
  28.         b += "x";
  29.    
  30.     // stampo  
  31.     std::cout << "Inserite stringhe a = " << a << " b = " << b << "\n";
  32.        
  33.     longString = (a.length() > b.length()) ? &a : &b;
  34.     shortString = (a.length() < b.length()) ? &a : &b;
  35.    
  36.     // controllo sottostringa
  37.     if (longString->find(*shortString) != std::string::npos)
  38.         std::cout << " La stringa piu' corta e' una sottostringa della scritta piu' lunga" << "\n";
  39.        
  40.     // controllo secondo carattere
  41.     size_t n = std::count(a.begin(), a.end(), b[1]);
  42.     std::cout << "il secondo carattere della stringa A e' presente " << n << ((n!=1)?"  volte":" volta")<<" nella stringa piu' lunga.\n";
  43.    
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement