我似乎无法理解为什么使用arr[i]!='\0'
永远不会执行它所假设的所有删除操作?我一直在使用与此类似的东西
/* This one is suppose to remove all the special characters & 0-9 from all entries */
void keepAlphaOnly(char line[50])
{
int i = 0, j;
for (i = 0; line[i] != '\0'; i++) {
if (line[i] < 65 || line[i] > 122 || (line[i] > 90 && line[i] < 97)) {
for (j = i; j <= strlen(line); j++) {
line[j] = line[j + 1];
}
--i;//EDIT: adjust index to avoid skipping next char
}
}
}
int main(void) {
int i, j, N;
char str[100][50];
scanf("%d\n", &N);
for (i = 0; i < N; ++i)
gets(str[i]);
for (i = 0; i < N; i++)
keepAlphaOnly(str[i]);
for (i = 0; i < N; i++)
printf("%s\n", str[i]);
return 0;
}
要删除某些字符或对字符串进行排序,搜索或删除,但它不会对整个字符串执行此操作,但会将其留在原始字符串之后?
答案 0 :(得分:0)
您尚未将空字符添加到“新”字符串中。
但你可以做到
int from = 0, to = 0;
for ( ;line[from]; ++from) {
if (isalpha(line[from])) {
line[to] = line[from];
to++;
}
}
line[to] = 0;