Code bài tập về chuỗi bị lỗi [Error] no matching function for call to ‘std::basic_string::replace'

Cho em hỏi bài này ở dòng

      if (kt1(s,i)==false&&kt3(s,i)==false&&kt3(s,i)==false&&kt4(s,i)==false&&kt5(s,i)==false) s.replace(s.find(i),' '); 

Nó cứ bảo là lỗi [Error] no matching function for call to ‘std::basic_string::replace(std::basic_string::size_type, char)’ là sao ạ. Mọi người giải thích giúp em với ạ. em cảm ơn nhiều :sunny:

#include <iostream>
using namespace std; 

bool kt1 (string s, int i) {  
    
       for (int j = 97; j<=122;j++) { 
          if (s[i]==j) return true; 
       }
    return false; 
}
bool kt2 (string s, int i) { 
       for ( int k = 65; k<=90;k++) { 
           if (s[i]==k) return true; 
       }
     return false;  
}
bool kt3 (string s, int i) { 
       if (s[i] == ' ') return true; 
        return false; 
}
bool kt4 (string s, int i) {
       if (s[i]== ',') return  true;
        return false; 
}
bool kt5 (string s, int i) { 
       if (s[i]== '?') return  true;
         return false; 
}
string questionCorrection(string s)
{
 for ( int i = 0; i<s.length();i++) {
      if (kt1(s,i)==false&&kt3(s,i)==false&&kt3(s,i)==false&&kt4(s,i)==false&&kt5(s,i)==false) s.replace(s.find(i),' '); 
 }
 while (s[0]== ' ') s.erase (0,1); 
 while (s[s.length()-1] == ' ') s.erase (s.length()-1,1); 
 while (s.find("  ")!=-1) { 
     s.erase (s.find("  "),1])
 }   
 return s; 
 }

int main ( ) { 
     string s; 
     getline (cin, s); 
    cout << questionCorrection(s); 
    return 0; 
}

Nên dùng mảng std::function :smiley: sau đó dùng <algorithm> :slight_smile:

3 Likes

sorry là sao ạ em không hiểu cả 2 cái :((

Có thể bạn truyền thiếu Agruments hoặc truyền sai ở s.replace(s.find(i),' '); :thinking:
http://www.cplusplus.com/reference/string/basic_string/replace/#:~:text=std%3A%3Abasic_string%3A%3Areplace&text=Copies%20str.&text=Copies%20the%20portion%20of%20str,sublen%20is%20basic_string%3A%3Anpos

5 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?