저는이 시점에서 상당히 고생하고 있습니다. 궁극적으로 나는 sentence
을 replaceSubstring
으로 보내고 "the"가 "that"로 바뀌면서 같은 문장을 돌려 보내고 싶습니다. 나는 포인터를 사용해야한다는 것을 알고 있지만, 정확히 어디서, 왜 그런지는 잘 모르겠습니다. 어떤 충고? 내가 갖는cstring 내의 단어를 바꿔 쓰려고 시도합니다.
오류는 다음과 같습니다
Ch12_08.cpp: In function ‘char replaceSubstring(char*, char*, char*)’:
Ch12_08.cpp:16: error: request for member ‘strstr’ in ‘sent’, which is of non-class type ‘char*’
Ch12_08.cpp:17: error: invalid conversion from ‘char*’ to ‘char’
Ch12_08.cpp:18: error: invalid conversion from ‘char*’ to ‘char’
Ch12_08.cpp: In function ‘int main()’:
Ch12_08.cpp:30: error: expected primary-expression before ‘]’ token
여기에 내가 함께 일하고 있어요 코드입니다 .. 사전에
#include <iostream>
#include <cstring> // Needed for strstr to work
using namespace std;
char replaceSubstring(char sent[], char oldW[], char newW[]){
char *strPtr = NULL;
strPtr = &sent.strstr(sent, oldW);
*strPtr = newW;
return sent;
}
int main()
{
char sentence[35] = "the dog jumped over the fence";
char oldWord[5] = "the";
char newWord[6] = "that";
char newSentence[35] = {NULL};
wcout << "The original sentence is: " << sentence << endl;
newSentence[] = replaceSubstring(sentence, oldWord, newWord);
return 0;
}
감사합니다!
덕분에이를하려는 이유를 궁금해. 그것은 수업을위한 것입니다. 왜 그래도 나는 궁금해. 나는 여분의 통찰력을 신경 쓰지 않는다. 그것은 우리가 배웠던 나머지 것들에 거의 직관력이없는 것처럼 보입니다. –
BTW,'oldW'의 _all_ 인스턴스를'newW'로 대체하거나 첫 번째 인스턴스 만 바꾸시겠습니까? 그 질문에 대한 답은 당신이해야 할 일을 바꿉니다. –