【发布时间】:2011-11-18 14:35:21
【问题描述】:
在学校作业中,我们被要求从字符串中删除所有出现的元音。
所以: “男孩踢球”会导致 "Th by kckd th bll"
每当找到元音时,所有后续字符都必须以某种方式向左移动,或者至少这是我的方法。由于我刚开始学习 C,很可能这是一种荒谬的方法。
我想要做的是:当我击中第一个元音时,我将下一个字符([i+1])“转移”到当前的位置(i)。然后必须为每个后续字符继续移位,因此 int startshift 设置为 1,因此第一个 if 块在每次后续迭代中执行。
第一个 if 块还测试下一个字符是否是元音。如果没有这样的测试,元音之前的任何字符都会“转换”为相邻的元音,并且除了第一个元音之外的每个元音仍然存在。然而,这导致每个元音都被前面的字符替换,因此 if else 块。
无论如何,这个丑陋的代码是我迄今为止想出的。 (用于 char* 指针的名称没有意义(我只是不知道如何称呼它们),并且拥有两组它们可能是多余的。
char line[70];
char *blank;
char *hlp;
char *blanktwo;
char *hlptwo;
strcpy(line, temp->data);
int i = 0;
int j;
while (line[i] != '\n') {
if (startshift && !isvowel(line[i+1])) { // need a test for [i + 1] is vowel
blank = &line[i+1]; // blank is set to til point to the value of line[i+1]
hlp = &line[i]; // hlp is set to point to the value of line[i]
*hlp = *blank; // shifting left
} else if (startshift && isvowel(line[i+1])) {
blanktwo = &line[i+1];
hlptwo = &line[i];
*hlptwo = *blanktwo;
//*hlptwo = line[i + 2]; // LAST MOD, doesn't work
}
for (j = 0; j < 10; j++) { // TODO: j < NVOWELS
if (line[i] == vowels[j]) { // TODO: COULD TRY COPY EVERYTHING EXCEPT VOWELS
blanktwo = &line[i+1];
hlptwo = &line[i];
*hlptwo = *blanktwo;
startshift = 1;
}
}
i++;
}
printf("%s", line);
代码不起作用。
带有文本.txt:
The boy kicked the ball
He kicked it hard
./oblig1 remove test.txt 产生: 那个男孩踢球了
e kicked it hard
注意。我省略了用于迭代文本文件中的行的外部 while 循环。
【问题讨论】:
-
很遗憾您的代码示例不完整;至少缺少一些函数签名。