Terminatorka Terminatorka - 1 month ago 21
C Question

Delete numbers from an array?

I am trying to make a program that asks the user to start typing different characters(doesn't matter what) until 'EOF'. After that I have to "printf" this array without the numbers.
So something like this:

'User input':123asd (! only example)

'Output':asd (! only example)


My problem is that I can't figure out the function. I was able to achieve:

User input:asd123 (!only example)

Output:asd (!only example)


But when I turn it around
(first example!)
it doesn't work at all.
Even something like you are stupid use pointers instead of this would be great. I am just trying to figure it if its possible this way!!

#include <stdio.h>

void element(char a[], int d) {
int i;

for (i = 0; i <d; i++) {
if (a[i] <= '9') {
/*
..........
*/
}
}
for (i = 0; i < d; i++) {
printf("%c", a[i]);
}
}

int main() {
char a[25];
int c, i, d;
i = 0;
d = 0;

while (i < 25) {
c = getchar();
if (c =='\n') {
a[i] != c;
} else if (c == EOF) {
break;
} else {
a[i] = c;
i++;
d++;
}
}
putchar('\n');

element(a, d);

return 0;
}

Answer

If you just want to print the character's, you do not need to delete them from the array. Just print the ones that are not integers.

void element(char a[], int d){
  int i;
  for(i=0;i<d;i++){
    if(!((a[i] >= '0') &&(a[i] <= '9')))
       printf("%c",a[i]);
  }
}    

If, however, you want to use the array of characters later, and you have no use of the array of integers, you can filter them out at the input stage. i.e. You use the if condition above at the time of reading the array and the array will only contain characters. You can then print normally.