----------------解决方案--------------------------------------------------------
用指针数组存放多个单词。当输入一个单词,在内存中查找该单词,并确认是否删除或替换。
要求:程序能够根据用户的需求,完成只删除(替换)一个或删除(替换)多个或删除(替换)所有的被查找的数据,给出被删除(替换)的数据个数,如果没有查找到,则给出没有查找到信息。
#include <string.h>
#define N 80
#define M 20
void append(char *str[N]); //添加单词函数
void delete(char *str[N]); //删除单词函数
void displace(char *str[N]); //替换单词函数
{
int n;
char *str[N]; //定义大小为N的指针数组
{
instruction();
printf("Please input your choice:\n"); //输入操作选择
scanf("%d", &n);
{
case 1:
append(&str[N]); //添加单词
break;
case 2:
delete(&str[N]); //删除单词
break;
case 3:
displace(&str[N]); //替换单词
break;
case 0:
exit(0); //退出
}
}
}
{
printf("1--添加单词\n");
printf("2--删除单词\n");
printf("3--替换单词\n");
printf("0--退出\n");
printf("\n\n");
}
{
int i = 0;
printf("Input the append word:");
scanf("%s", &str[i ++]);
}
void delete(char *str[N]) //删除单词
{
int i, k, n;
char key[M], *p, *q;
printf("Input the delete word:"); //输入要删除的单词
scanf("%s", key);
while (1)
{
if (strcmp(str[i], key) != 0) //未找到 k = 0
{
k = 0;
i ++;
continue;
}
else //找到 k = 1
{
k = 1;
break;
}
}
if (!k)
{
printf("The word is not found!\n");
}
else
{
printf("The word is found!\n");
printf("Are you sure delete this word?\n"); //确定是否要删除
printf("1----delete\n");
printf("2----not delete\n");
scanf("%d", &n);
}
if (n == 1)
{
p = str[i];
for (q = str[i]; ; q ++) //移动元素
{
*(q + 1) = *q;
}
}
else
exit(0);
}
void displace(char *str[N]) //替换单词
{
int i, k, n;
char key[M], *newstr;
printf("Input the displace word:"); //输入要替换的单词
scanf("%s", key);
for (i = 0; ; i ++)
{
if (strcmp(str[i], key) != 0) //未找到 k = 0
{
k = 0;
continue;
}
else //找到 k = 1
{
k = 1;
break;
}
}
if (!k)
{
printf("The word is not found!\n");
}
else
{
printf("The word is found!\n");
printf("Are you sure displace this word?\n"); //确定是否要替换
printf("1----displace\n");
printf("2----not displace\n");
scanf("%d", &n);
}
if (n == 1)
{
printf("Input new word:"); //输入新单词
scanf("%s", &newstr);
strcpy(str[i], newstr);
}
else
exit(0);
}
----------------解决方案--------------------------------------------------------