Usuwanie jednakowych liter z tekstu

0

Mam prosty program do napisania, ma wczytac tekst od uzytkownika a nastepnie usunac z niego litere 'a'.

Program się kompiluje i w sumie działa, ale tylko dla jednego wyrazu. Np.

I: 'Ala ma kota'
O: 'l ma kota'

Dla 'ma' i 'kota' juz nie dziala... Pewnie coś w tej pętli popsułem, macie pomysły jak to naprawić?

 
#include <iostream> 
#include <string> 

using namespace std; 

void usunL(string &tekst, char c) 
{
   size_t pozycjaL = tekst.find(c); 
 
   if(pozycjaL == string::npos) 
      cout << "Nie ma w ogole takiej litery w tekscie!" << endl; 
   else 
     do{
         tekst.erase(pozycjaL, pozycjaL); // usuwanie literki 
         pozycjaL = tekst.find(c, pozycjaL + 1); // przesuwam miejsce od ktorego zaczyna szukac petla literki
         }while(pozycjaL  == string::npos)   
     cout << tekst; 

} 

int main()
{
   string tekst; 
   char c = 'a'; 
   
   getline(cin, tekst); 
   usunL(tekst, c); 
   return 0; 
} 

2
while(pozycjaL  != string::npos)

oraz

tekst.erase(pozycjaL, 1);
1
size_t pos;
while((pos = tekst.find(c)) != string::npos) tekst.erase(pos, 1);
0
#include <iostream>
#include <string>
using namespace std; 

bool removeChar(string &str,char ch) 
  {
   size_t len=str.length();
   for(size_t prev=0,pos;(pos=str.find(ch,prev))!=string::npos;prev=pos) str.erase(pos,1);
   return len!=str.length();
  }
 
int main()
  {
   string line; 
   getline(cin,line); 
   if(removeChar(line,'a')) cout<<line<<endl;
   else cout<<"Nie ma w ogole takiej litery w tekscie!"<<endl; 
   return 0; 
  }
2
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>

using namespace std;
void usunL(string &tekst, char c)
{
	string result;
	remove_copy_if(begin(tekst), end(tekst), back_inserter(result), bind1st(equal_to<char>{}, c));
	tekst = result;
}

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