我试图编写一个C程序来删除字符串中除最后一次出现以外的所有重复出现的字符。例如,如果我有字符串
char word[]="Hihxiivaeiavigru";
输出应为:
printf("%s",word);
hxeavigru
到目前为止,我有:
#include <stdio.h>
#include <string.h>
int main()
{
char word[]="Hihxiiveiaigru";
for (int i=0;i<strlen(word);i++){
if (word[i+1]==word[i]);
memmove(&word[i], &word[i + 1], strlen(word) - i);
}
printf("%s",word);
return 0;
}
我不确定自己在做什么错。
我首先要编写一个函数来确定achar ch
在给定位置i
是否是ch
给定a的最后一次出现char *
。喜欢,
bool isLast(char *word, char ch, int p) {
p++;
ch = tolower(ch);
while (word[p] != '\0') {
if (tolower(word[p]) == ch) {
return false;
}
p++;
}
return true;
}
然后,您可以使用它来反复发出所需的字符,例如
int main() {
char *word = "Hihxiivaeiavigru";
for (int i = 0; word[i] != '\0'; i++) {
if (isLast(word, word[i], i)) {
putchar(word[i]);
}
}
putchar('\n');
}
而且(为了完整性),我使用了
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
输出(根据要求)
hxeavigru
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句