-1
void strip_out(char *str) {


    int length_string = strlen(str);
    //printf("%d\n", length_string);
    char temp[length_string];
    int i = 0;
    int j = 0;
  while ((*(str + i) != '\0')) {

    if (isalpha(str[i])) {
            //printf("yes\n");
           *(temp + j) = *(str + i);
            i++;
            j++;
            }
        else {
            //printf("No\n");
            i++;
            }


     }

I need my *str value to be changed to the value of temp so i can use it in another function.

I cannot change the return type, if i could I could just return temp but it has to be void.

manlio
  • 18,345
  • 14
  • 76
  • 126

2 Answers2

0

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
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Considering that you cannot change the return type, you could:

  • add a final call to strcpy;
  • fix the missing string null character ('\0') issue. Without the terminal strcpy will keep processing data past the end of the temp character array until it happens to find a zero byte in memory.
void strip_out(char *str)
{
  char temp[strlen(str)];
  int j = 0;

  for (int i = 0; str[i]; ++i)
    if (isalpha(str[i]))
    {
      temp[j] = str[i];
      ++j;
    }
  temp[j] = '\0';

  strcpy(str, temp);
}

For a educational purpose the code intentionally is as similar as possible to the original one.

An in place transformation (the first part of Vlad's answer) should probably be preferred.

manlio
  • 18,345
  • 14
  • 76
  • 126