Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- #include <algorithm>
- std::string RequestString(int length, std::string nome)
- {
- std::string result;
- while (result.length() <= length)
- {
- std::cout << "Inserisci un testo "<< nome <<" di lunghezza superiore a " << length << "\n";
- std::cin >> result;
- }
- return result;
- }
- int main()
- {
- // stringhe
- std::string a = RequestString(3, "A");
- std::string b = RequestString(3, "B");
- std::string* longString;
- std::string* shortString;
- // controlli lunghezze
- if (a.length() == b.length())
- b += "x";
- // stampo
- std::cout << "Inserite stringhe a = " << a << " b = " << b << "\n";
- longString = (a.length() > b.length()) ? &a : &b;
- shortString = (a.length() < b.length()) ? &a : &b;
- // controllo sottostringa
- if (longString->find(*shortString) != std::string::npos)
- std::cout << " La stringa piu' corta e' una sottostringa della scritta piu' lunga" << "\n";
- // controllo secondo carattere
- size_t n = std::count(a.begin(), a.end(), b[1]);
- std::cout << "il secondo carattere della stringa A e' presente " << n << ((n!=1)?" volte":" volta")<<" nella stringa piu' lunga.\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement