Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: C++  |  size: 1.61 KB  |  hits: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6. bool sottostring(string S1, string S2){
  7.  
  8.     bool substring=false;
  9.  
  10.     int i=0;
  11.     int L1=S1.length();
  12.     int L2=S2.length();
  13.     while( i<=(L1-L2) && !substring)
  14.     {
  15.         if(S1.substr(i,L2)==S2)
  16.         substring = true;
  17.  
  18.         else
  19.         i++;
  20.  
  21.     }
  22.     return substring;
  23. }
  24.  
  25.  
  26. int main()
  27. {
  28. cout<<"lavorare con le stringhe:\n";
  29. cout<<"INERIRE\n"<<"1)sottotringa se vuoi vedere se una stringa e' sottostringa di un'altra;\n"<<"2) somma se vuoi sommare due stringhe;\n";
  30. string S1;
  31. string S2;
  32. string SL1;
  33. string SL2;
  34. string input;
  35.  
  36. int sottostringa = 12;
  37. int B = sottostringa;
  38. int somma = 5;
  39. int C = somma;
  40.  
  41. cout<<"inserisci consegna:\n";
  42.  
  43. cin>>input;
  44.  
  45. int A= input.length();
  46.  
  47. cout<<endl;
  48.  
  49.     if(A == B)
  50.     {
  51.         cout<<"inserisci S1:\n";
  52.         getline(cin, S1);
  53.  
  54.         cout<<endl;
  55.  
  56.         cout<<"inserisci S2:\n";
  57.         getline(cin,S2);
  58.  
  59.         if(sottostring(S1,S2))
  60.         {
  61.         cout<<S2<<" e' sottostringa di "<<S1;
  62.  
  63.         cout<<"\n";
  64.  
  65.         }else{
  66.  
  67.             cout<<S2<<" non e' sottostringa di "<<S1;
  68.         }
  69.  
  70.  
  71.  
  72.     }
  73.     if(A == C )
  74.     {
  75.         cout<<"inserisci SL1:\n";
  76.         cin>>SL1;
  77.         cout<<endl;
  78.         cout<<"inserisci SL2:\n";
  79.         cin>>SL2;
  80.  
  81.         string SS3;
  82.  
  83.         int LL1=SL1.length();
  84.         int LL2=SL2.length();
  85.         int LL3=LL1+LL2;
  86.  
  87.         SS3=SL1+SL2;
  88.  
  89.         cout<<"l'unione tra "<<SL1<<" e "<<SL2<<"  e':   "<<SS3<<";\n";
  90.         cout<<"la lunghezza di "<<SS3<<" e' "<<LL3<<";\n";
  91.     }
  92.  
  93.  
  94.     return 0;
  95. }