사용자가 지정한 문자열을 구문 분석하고 이전 부분 문자열의 모든 항목을 새 문자열로 바꾸려면 어떻게해야합니까? 나는 함께 작업 할 수있는 기능을 가지고 있지만 문자열에 관해서는 정말로 확신 할 수 없습니다.구문 분석 문자열 및 스왑 부분 문자열
void spliceSwap(char* inputStr, const char* oldStr, const char* newStr )
사용자가 지정한 문자열을 구문 분석하고 이전 부분 문자열의 모든 항목을 새 문자열로 바꾸려면 어떻게해야합니까? 나는 함께 작업 할 수있는 기능을 가지고 있지만 문자열에 관해서는 정말로 확신 할 수 없습니다.구문 분석 문자열 및 스왑 부분 문자열
void spliceSwap(char* inputStr, const char* oldStr, const char* newStr )
가장 간단한 해결책은 여기에 google (First link)을 사용하는 것입니다. 또한 C++에서 const char *
보다 std::string
을 선호합니다. 자신의 std::string
을 쓰지 마십시오. 내장 된 것을 사용하십시오. 귀하의 코드는 C보다 더 C 것 같습니다 + +!
// Zammbi's variable names will help answer your question
// params find and replace cannot be NULL
void FindAndReplace(std::string& source, const char* find, const char* replace)
{
// ASSERT(find != NULL);
// ASSERT(replace != NULL);
size_t findLen = strlen(find);
size_t replaceLen = strlen(replace);
size_t pos = 0;
// search for the next occurrence of find within source
while ((pos = source.find(find, pos)) != std::string::npos)
{
// replace the found string with the replacement
source.replace(pos, findLen, replace);
// the next line keeps you from searching your replace string,
// so your could replace "hello" with "hello world"
// and not have it blow chunks.
pos += replaceLen;
}
}
숙제가 있습니까? – 0x499602D2
지금까지 시도한 코드를 볼 수 있습니까? – Borgleader
이것은 C 또는 C++에 있습니까? C++이라면'std :: string'을 사용하십시오. C 언어 인 경우 적절하게 태그를 지정하십시오. –