0
我在这一点上很困难。最终,我想发送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;
}
谢谢!
感谢您的答复。这是一个班级。我不知道为什么,尽管..虽然我不介意额外的洞察力。这似乎与我们学习的其他东西几乎是不直观的。 –
顺便说一下,你应该用'newW'或者只是第一个来替换'oldW'的_all_实例吗?这个问题的答案改变了你需要做的工作。 –