Вы можете изменить его на месте:
#include <ctype.h>
#include <stdio.h>
int main() {
char s[] = "!!Hello**";
size_t pos = 0;
for (char *p = s; *p; ++p)
if (isalpha(*p))
s[pos++] = *p;
s[pos] = '\0';
printf("'%s'\n", s);
}
выход
'Hello'
Или используя только указатели:
#include <ctype.h>
#include <stdio.h>
void filter_alpha(char *s) {
for (char *p = s; *p; ++p)
if (isalpha(*p))
*s++ = *p;
*s = '\0';
}
int main() {
char s[] = "!!Hello**";
filter_alpha(s);
printf("'%s'\n", s);
}
Чтобы удалить только начальные / конечные не-буквенные символы
#include <assert.h>
#include <ctype.h> // isalpha()
#include <stdio.h>
#include <string.h> // strlen()
char* strip_nonalpha_inplace(char *s) {
for ( ; *s && !isalpha(*s); ++s)
; // skip leading non-alpha chars
if (*s == '\0')
return s; // there are no alpha characters
assert(isalpha(*s));
char *tail = s + strlen(s);
for ( ; !isalpha(*tail); --tail)
; // skip trailing non-alpha chars
assert(isalpha(*tail));
*++tail = '\0'; // truncate after the last alpha
return s;
}
int main() {
char s[] = "!!Hello**";
printf("'%s'\n", strip_nonalpha_inplace(s));
}