通过strcpy删掉字符串数组怎么解决
现在要通过add添加字符串数组,用remove删掉字符串,怎么删阿,我用strcpy应该没问题啊,怎么不管用,大哥们帮我看一下。
例:添加完后。用display
1.hsuhwidhwi
2.wjidwdjjdo
3.wdihh
若删掉第二个,应该是
1.hsuhwidhwi
2.wdihh
#include "stdio.h "
#include <iostream>
using namespace std;
char rulestr[20][100];
int rule_no;
void add_rule()
{
cout < < "Please type in the rules:\n ";
gets(rulestr[rule_no]);
rule_no++;
}
void remove_rule()
{
cout < < "Please select the number to remove\n ";
int remove_no;
cin> > remove_no;
int mov;
for(mov=remove_no;mov <rule_no;mov++)
{strcpy(rulestr[mov],rulestr[mov+1]);
}
rule_no=rule_no-1;
}
void disp_rule()
{
cout < < "Display all the rules!\n ";
int rule_dis;
for(rule_dis=1;rule_dis <=rule_no;rule_dis++)
{
cout < <rule_dis < < ": " < <rulestr[rule_dis-1] < < "\n ";
}
}
void analyze_rule()
{}
void main()
{
cout < < "(1). Add a filter rule:\n ";
cout < < "(2). Remove a filter rule:\n ";
cout < < "(3). Display filter rules:\n ";
cout < < "(4). Analyze filter rules:\n ";
cout < < "(5). Exit.\n ";
int comm_no;
do {cin> > comm_no;
char temp[128];
gets(temp);
if(comm_no==1) add_rule();
if(comm_no==2) remove_rule();
if(comm_no==3) disp_rule();
if(comm_no==4) analyze_rule();
} while (comm_no!=5);
}
[解决办法]
strcpy不能删除字符串,你只是把后面的字符串依次拷贝到前一个。最后一个,就是下标为rule_no-1的最好清空掉。还有你要删掉第二个的话,如果输入的是2,记得对应的下标是1,remove_no应该是从remove_no-1开始
[解决办法]
注意 数组的索引是从 0 开始的,
所以,
输入的 第几个字符串,
对应的索引是 输入值-1
如上修改后,
程序可以达到楼主你的要求 ~