如何修复此代码的限制
how to fix the limitation of this code
在这段代码中,我想将一个单词搜索到字符串行中,如果找到了单词,我想反转该单词。例如:This is c++ language
这是字符串行,我想知道单词c++
是否存在。如果存在,则打印该行CCD_ 3。所以在这一行中,单词从c++变成了++c。我该怎么做。
# include<iostream>
using namespace std;
int main() {
int i, j,flage=0,count=0;
char a[] = { "This is bangladesh." };
char b[788];
cout<<"Before searchingn";
cout<<"****************************"<<endl;
for (int m=0; a[m]!=' ';m++)
{
cout<<a[m];
}
cout<<endl;
cout<<"Write what you want to search from above line: ";
cin>>b;
cout<<endl;
cout<<"after searching n";
cout<<"*****************************"<<endl;
for (int p=0; b[p]!=' ';p++)
{
count++;
}
for (i = 0; a[i] != ' '; i++)
{
for (j = 0; b[j] != ' '; j++)
{
if (a[j + i] != b[j])
break;
}
if (b[j] == ' ')
flage = 1;
}
if (flage == 1)
{
cout << "This is a ";
for (i =count; i >= 0; i--)
cout << b[i];
}
cout<<"nnnn";
return 0;
}
我认为这是一项学习任务,所以让您使用现有函数是不可能的。。。
考虑到这一点,将您的问题划分为多个函数:首先,你需要找到你要找的单词的起始索引。找到起始索引后,找到最后一个索引(这应该很容易)现在,开始交换:将第一个索引的内容与最后一个索引交换,将第一个向前移动一步,将最后一个向后移动一步并再次交换。这样做,直到你交换了所有的单词。
如果可以的话,还有一件事,给你的变量更多的描述性名称。"a"answers"b"并不是真正的描述性。。。
试试这个
void reverse(char *x, int begin, int end)
{
char c;
if(begin>=end)return;
c=*(x+begin);
*(x+begin)=*(x+end);
*(x+end)= c;
reverse(x, ++begin, --end);
}
int main()
{
int i, j,flage=0,count=0;
char a[] = { "This is bangladesh." };
char b[788];
cout<<"Before searchingn";
cout<<"****************************"<<endl;
cout<<a;
cout<<endl;
cout<<"Write what you want to search from above line: ";
cin>>b;
cout<<endl;
cout<<"after searching n";
cout<<"*****************************"<<endl;
char *ptr=strstr(a,b);
// in "This is bangladesh" if you search for "is" it will come twice.. so check for word
while(ptr && *(ptr-1)!=' ') //search for word
{
ptr+=strlen(b);
ptr=strstr(ptr,b);
}
if(ptr)
{
reverse(b,0,strlen(b)-1);
cout<<"string foundn";
memcpy(ptr,b,strlen(b));
cout<<a;
}
else
cout<<"string not foundn";
cout<<"nnnn";
return 0;
}
# include<iostream>
using namespace std;
int main() {
int i, j,flage=0,count=0;
char a[] = { "This is bangladesh." };
char b[788];
cout<<"Before searchingn";
cout<<"****************************"<<endl;
for (int m=0; a[m]!=' ';m++)
{
cout<<a[m];
}
cout<<endl;
cout<<"Write what you want to search from above line: ";
cin>>b;
cout<<endl;
cout<<"after searching n";
cout<<"*****************************"<<endl;
for (int p=0; b[p]!=' ';p++)
{
count++;
}
for (i = 0; a[i] != ' '; i++)
{
for (j = 0; b[j] != ' '; j++)
{
if (a[j + i] != b[j])
break;
}
if (b[j] == ' ')
flage = 1;
}
if (flage == 1)
{
cout << "This is a ";
for (i =count; i >= 0; i--)
cout << b[i];
}
else //Reverse the string
{
char *p = b[i], *s = b[i] + strlen(b[i]) - 1;
while (p < s) {
char tmp = *p;
*p++ = *s;
*s-- = tmp;
}
}
cout<<"nnnn";
return 0;
}
相关文章:
- 如何修复sfml c++代码编译错误
- 如何修复我的最大公约数代码?它适用于除零和零以外的所有数字
- 我的代码中的意外价值以及我如何修复它
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- C++指针问题:如何修复这些代码中的错误?
- 如何修复 Arduino LED 串控制器的代码?
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 如何修复初学者C++代码中发生的未定义行为
- 终端进程终止,退出代码为:1有什么方法可以修复它吗
- 我在哪里修复有关模板的C++代码?
- 无论代码长度如何,以下代码的内存要求有何不同?
- 如何修复以下代码以进行二叉树级别订单打印
- 如何在以下代码中修复此逻辑错误
- 如何修复此代码对几个测试用例的错误答案?
- 修复第三方代码:"error: ‘enable_if’ in namespace ‘std’ does not name a template type"
- 如何修复将传输到 java 的 2d 数组代码使用 JNI
- 如何修复代码中的"Invalid memory reference"错误?
- 如何修复使用代码 2 退出"CL.exe"
- 如何在我的代码中修复无穷循环?我认为它跳过了第二个CIN,因此它一直在循环
- 如何在导致应用程序崩溃的代码中修复指令