Algorytmy i String

0

Witam mam 4 zadanka za które nie bardzo wiem jak mam się zabrać czy potrafiłby ktoś ogolnie nakreślić jak się za nie zabrać ?

 Zadanie 4A. Wykorzystując klasę string i inny algorytm usuń ze string wszystkie znaki inne różne od dużych liter.


Zadanie 4C   Wykorzystując algorytmy, sprawdź, czy teksty, które zostały przekazane do aplikacji jako argumenty linii poleceń składają się dokładnie z tych samych znaków i mają taką samą długość.

4D  Wykorzystując klasę string i algorytm all_off, sprawdź, czy tekst zapisany zmiennej typu string jest w którym nie ma małych liter.
0

W czym masz to napisać??

0

c++
w 4A zastosowałbym find if. a nastepnie erase na danym elemencie stringa ale cos mi tu nie dziala....

 #include <iostream>
#include <algorithm>
using namespace std;

bool isEven( char x)
{
    return islower(x);
}

int main()
{
    string word = "teSt";
  
    
    int * it = find_if( word.begin(),word.end(), isEven 	);
   
   cout<<*it ; 
}
2

Ad 4A. remove_if + funktor/funkcja + resize
Ad 4C. sort + funktor/funkcja + size (może jeszcze unique + resize jeżeli "aab" == "bba" )
Ad 4D. all_off + funktor/funkcja

2

w 4A zastosowałbym find if. a nastepnie erase na danym elemencie stringa ale cos mi tu nie dziala....

No tak, bo błędów tutaj sporo (w komentarzach)

#include <iostream>
#include <algorithm>
using namespace std;
 
bool isEven( char x)
{
    return islower(x); // islower jest w <cctype> który nie jest dołączony
}
 
int main()
{
    string word = "teSt";
 
 
    int * it = find_if( word.begin(),word.end(), isEven     );
    // po pierwsze, int * it? Nic z algorytmów nie zwraca wskaźników, o to chodzi w iteratorach
    // po drugie, czemu find_if? 
    // http://www.cplusplus.com/reference/algorithm/find_if/
    // Returns an iterator to the first element in the range [first,last) for which pred returns true. If no such element is found, the function returns last.
    // Chyba tego jednak nie przemyślałeś (albo wybraleś losową funkcję?). W każdym razie, Tobie chodzi o remove_if
    // http://www.cplusplus.com/reference/algorithm/remove_if/
 
   cout<<*it ; // no i ostatnia rzecz, co to ma zrobić?
   // 1) Rzeczy z algorithm jeśli coś zwracają, to jest to często np. iterator do ostatniego elementu (find do znalezionego elementu albo końca).
   // 2) Nie możesz tak wypisać iteratora (tzn. jeśli to miało wypisać tekst po usunięciu niedużych liter, bo pojedynczy element jak najbardziej można tak wyświetlić 
}

To moja propozycja rozwiązania:

#include <iostream>
#include <cctype>
#include <algorithm>

using namespace std;
 
bool isNotUpper(char x){ return !isupper(x); }
 
int main()
{
    string word = "teStAsRsa";
 
    string::iterator begin = word.begin(), end = word.end();
    end = remove_if(begin, end, isNotUpper);
 
    for (string::iterator it = begin; it != end; it++) {
        cout << *it;
    } cout << endl;
}
0

Wielkie dzęki. analogicznie zrobiłem program z algorytmem all_of ale dev wyrzuca mi że nie zna "all_of"

'all_of' was not declared in this scope

 #include <iostream>
#include <cctype>
#include <algorithm>
 
using namespace std;
 
bool isNotUpper(char x){ return isupper(x); }
 
int main()
{
    string word = "test";
 	
    string::iterator begin = word.begin(), end = word.end();
    if (all_of(begin,end,isNotUpper) )cout<<"nie ma malych liter"<<endl;
 	else cout<<"sa male liter"<<endl;
}
0
  1. 4A:
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;

bool boolislower(char ch) { return islower(ch); }

int main ()
  {
   string str("teStAsRsa");
   str.resize(remove_if(begin(str),end(str),&boolislower)-begin(str));
   cout<<str<<endl;
   return 0;
  }

lub:

    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
     
    int main ()
    {
    string str("teStAsRsa");
    str.resize(remove_if(begin(str),end(str),[](char ch) { return islower(ch); } )-begin(str));
    cout<<str<<endl;
    return 0;
    }
  1. 4D:
#include <iostream>
#include <cctype>
#include <algorithm>
using namespace std;
 
bool boolislower(char ch) { return islower(ch); }
 
int main()
  {
   string word = "test";
   if(all_of(word.begin(),word.end(),boolislower)>=word.size()) cout<<"nie ma malych liter"<<endl;
   else cout<<"sa male litery"<<endl;
   return 0;
  }

lub:

#include <iostream>
#include <cctype>
#include <algorithm>
using namespace std;
 
int main()
  {
   string word = "test";
   if(all_of(word.begin(),word.end(),[](char ch) { return islower(ch); } )>=word.size()) cout<<"nie ma malych liter"<<endl;
   else cout<<"sa male litery"<<endl;
   return 0;
  }

1 użytkowników online, w tym zalogowanych: 0, gości: 1