0

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;
}
2
  • 1
    if(a[i] <= '9') --> if ((a[i] < '0') || (a[i] > '9')) Commented Nov 9, 2016 at 11:09
  • @WhozCraig Right! Edited. Commented Nov 9, 2016 at 11:14

7 Answers 7

2

In your void element(char a[], int d) function just put the following code -->>

          int i;

            for(i=0;i<d;i++){
                    if(a[i] > '9' || a[i]< '0')
                    {
                        printf("%c",a[i]);

                    }

            }

The above code will print all the characters other than Numbers (0-9).And if you want to remove the Special characters also then Do refer to the ASCII values of the special characters and mention it in your code

Sign up to request clarification or add additional context in comments.

Comments

1

you can use isalpha to check for a letter-character

#include <stdio.h>
#define MAX_SIZE 25

int main(){

        char a[MAX_SIZE];
        int i = 0,d = 0;
        char c;

        do {
            c = getchar();
            if (isalpha(c)) {
                a[d++] = c;
            }
            ++i;
        } while (c != EOF && i < MAX_SIZE);
        putchar('\n');

        for(i = 0; i < d; ++i) {
            printf("%c",a[i]);
        }

        return 0;
}

Comments

0

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.

2 Comments

Fixed it. Thanks @LPs
Not yet. You should delete = from conditions and && should be ||
0

The easiest way is to skip numbers at the input stage:

        while(i<25){
                c=getchar();
                if(c =='\n'){
                        /* I don't know what you are trying to do here */
                        a[i] != c; 
                }else if(c == EOF){
                        break;
                }else if(c >= '0' && c <= '9'){
                    /* number - skip it */
                }else{
                        a[i] = c;
                        i++;
                        d++;
                }
        }

2 Comments

I would have said "at the output stage". Just in case the non-numeric characters are actually wanted. This fulfills the demand, since OP just want to print the values but the numbers.
I wanted to skip '\n' otherwise it would put it in my char array and I didn't like it because I would loose half of the entered data. Thanks for the answer.
0

You can simply do

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]);
      }
   }
}

Comments

0
#include <stdio.h>
#include <ctype.h>

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

    for(j = i = 0; i < *d; i++){
        if(!isdigit((unsigned char)a[i])){
            a[j++] = a[i];
        }
    }
    *d = j;//Reflect size change by deletion
    for(i = 0; i < *d; i++){
        printf("%c", a[i]);
    }
    putchar('\n');
}

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

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

    putchar('\n');
    element(a, &d);

    return 0;
}

Comments

0

All the answers are giving me what I want to see.Thank you for all the different approaches and helping me in learning! @Klas Lindback , @samarth kejriwal,@myxaxa, @BLUEPIXY, @LPs, @Rishikesh Raje

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.