userxbw

Finds substrings C++

Sep 7th, 2022
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. using std::cin,std::cout,std::endl,std::string;
  5. // -std=c++17
  6.  
  7. bool is_upper(string *word,int el)
  8. {
  9.     for(int c=0;c<word[el].size();c++){
  10.         if(std::isupper(static_cast<unsigned char>(word[el][c]))){                  return true;
  11.         }
  12.  
  13.     }
  14.     return false;
  15. }
  16.  
  17. void start_mess(){
  18. cout
  19. <<"This program seeks for a substring within"
  20. <<" another string. First enter the sub-string"
  21. <<" then the string to be searched."
  22. <<endl
  23. <<endl
  24. <<"BUT FIRST:"
  25. <<endl
  26. <<"Enter amount of test cases to be done"<<endl;
  27. }
  28. void sub_string_mess(){
  29.     cout<<"enter possable substring"<<endl;
  30.     }
  31. void search_string_mess(){
  32.     cout<<"enter string to be searched"<<endl;
  33.     }
  34.  
  35. void upper_case_err_mess(){
  36.     cout<<endl<<"ALL TEXT HAS TO BE <lowercase>!"
  37.     <<endl
  38.     <<"and or under 1001 chars."
  39.     <<endl
  40.     <<"You figure out what you did wrong. I'm just a computer."
  41.     <<endl<<endl;
  42.     }
  43.  
  44. int main(){
  45. /* MAX TEST 100 */
  46. string strA[100]={"SCOOBIE"},strB[100]={"DOO"};
  47. string sub,search;
  48. int test_case=0,tested=1;
  49. char testing='y';
  50.     int ct2=0;
  51.    int ct1=0;
  52.  
  53. // get amount of test control loop
  54. while(testing == 'y'){
  55.  
  56. start_mess();
  57. cin>>test_case;
  58.  
  59.    if(!cin){
  60.         std::cin.clear();
  61.         std::cin.ignore(std::numeric_limits<
  62.         std::streamsize>::max(), '\n');
  63.         std::cerr<<endl<<"Bad input"<<endl<<endl;
  64.         continue;
  65.     }
  66.     else if(test_case<=0 || test_case>100){
  67.     cout<<"does not meet the requirments\n"
  68.     <<"limits 1-100 tests"<<endl;
  69.     continue;
  70.     } // end i
  71.     else{
  72.  
  73.         testing='n';
  74.    } // end else
  75.  
  76. } // end testing loop
  77.  
  78. // get strings control loop
  79. char gs='y';
  80. while(gs=='y'){
  81.  
  82.    char sbs='y';
  83.    while(sbs=='y'){
  84.  
  85.     //sub_string_mess();
  86.  
  87.     /*skips over these functions */
  88.     //getline(cin,strA);
  89.     //cin.getline(strA,1000);
  90.     cin>>strA[ct1];
  91.         if(is_upper(strA,ct1) ||
  92.         strA[ct1].size()>1000 ||
  93.         strA[ct1].size()<1) {
  94.             upper_case_err_mess();
  95.             continue;
  96.         }
  97.         else{
  98.          sbs='n';
  99.          }
  100.     }// end sub string for
  101.  
  102.  
  103.     char ss='y';
  104.     while(ss=='y'){
  105.  
  106.         //search_string_mess();
  107.  
  108.             /* skips over these functions */
  109.             //cin.getline(strB,1000);
  110.             //getline(cin,strB);
  111.         cin>>strB[ct2];
  112.             if(is_upper(strB,ct2)   ||
  113.                 strB[ct2].size()<1  ||
  114.                 strB[ct2].size()>1000){
  115.                 upper_case_err_mess();
  116.                 continue;
  117.             } // end if
  118.             else
  119.                 ss='n';
  120.    } // end for
  121.  
  122.     // stop loop check
  123.     if((tested++)>=test_case){
  124.         gs='n';
  125.    }
  126.     else{ // reset srting gathering
  127.         sbs='y';
  128.         ss='y';
  129.         ct1++;
  130.         ct2++;
  131.    } // end else
  132. } // end control loop
  133.  
  134. cout<<endl<<endl<<endl;
  135.  
  136. for(int a=0;a<test_case;a++){
  137. /*
  138. cout<<endl<<strA[a]<<endl
  139. <<strB[a]<<endl;
  140. */
  141.     strB[a].find(strA[a]) != string::npos ?
  142.     cout<<"YES"<<endl:cout<<"NO"<<endl;
  143.  
  144. }
  145. cout<<"one more time using a different methiod"<<endl<<endl;
  146.  
  147. for(int d=0;d<test_case;d++){
  148. strB[d].find(strA[d].c_str(), strA[d].size()) != string::npos ?
  149.         cout
  150.         <<endl
  151.         << " Yep "
  152.         <<endl :
  153.         cout << " nope "
  154.         << endl;
  155. }
  156.  
  157.  
  158.  
  159. return 0;
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment