【发布时间】:2014-05-04 03:23:13
【问题描述】:
我在课堂上有一个练习题让我很困惑,那就是编写一个名为 strCut 的函数,它接收两个 C 风格的字符串参数 s 和模式。如果模式字符串包含在 s 中,则该函数修改 s,以便从 s 中删除出现在 s 中的第一次出现的模式。要执行模式搜索,请使用预定义的 strstr 函数。
这是我现在的代码。
void strCut(char *s, char *pattern)
{
char *x;
char *y;
x = strstr(s, pattern);
cout << x; // testing what x returns
}
int main()
{
char s[100]; // the string to be searched
char pattern[100]; // the pattern string
char response; // the user's response (y/n)
do
{
cout << "\nEnter the string to be searched ==> ";
cin.getline(s, 100);
cout << "Enter the pattern ==> ";
cin.getline(pattern, 100);
strCut(s, pattern);
cout << "\nThe cut string is \"" << s << '"' << endl;
cout << "\nDo you want to continue (y/n)? ";
cin >> response;
cin.get();
} while (toupper(response) == 'Y');
非常感谢任何帮助。谢谢
【问题讨论】:
-
由于这似乎是家庭作业,我只提供一个提示:
strstr完成了一半的工作。其余工作将由memmove完成。 (可能显示memcpy或strcpy也可以完成该部分,但这是不正确的。)您还需要strlen。
标签: c++ function strcpy strcat strstr