If you want to change a string in place removing all non alpha characters then there is no need to define an auxiliary array.
The function can look the following way as it is shown in the demonstration program below.
#include <stdio.h>
#include <ctype.h>
char * strip_out( char *s )
{
char *p = s;
for ( char *q = s; *q != '\0'; ++q )
{
if ( isalpha( ( unsigned char )*q ) )
{
if ( p != q ) *p = *q;
++p;
}
}
*p = '\0';
return s;
}
int main(void)
{
char s[] = "1H2e3l4l5o";
puts( s );
puts( strip_out( s ) );
return 0;
}
The program output is
1H2e3l4l5o
Hello
If you want to create a new string based on the passed argument then the function can look the following way as it is shown in the next demonstrative program.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char * strip_out( const char *s )
{
size_t n = 0;
for ( const char *q = s; *q != '\0'; ++q )
{
if ( isalpha( ( unsigned char ) *q ) ) ++n;
}
char *result = malloc( n + 1 );
if ( result != NULL )
{
char *p = result;
for ( const char *q = s; *q != '\0'; ++q )
{
if ( isalpha( ( unsigned char )*q ) )
{
*p++ = *q;
}
}
*p = '\0';
}
return result;
}
int main(void)
{
const char *s = "1H2e3l4l5o";
puts( s );
char *p = strip_out( s );
if ( p != NULL ) puts( p );
free( p );
return 0;
}
The program output is the same as shown above
1H2e3l4l5o
Hello