从数组中删除数字?

时间:2016-11-09 11:01:44

标签: c arrays

我正在尝试制作一个程序,要求用户开始输入不同的字符(并不重要)直到' EOF'。在那之后,我必须" printf"这个数组没有数字。 所以像这样:

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

'Output':asd (! only example)

我的问题是我无法弄清楚这个功能。我能够实现:

User input:asd123   (!only example)

Output:asd          (!only example)

但当我转过(first example!)时,它根本不起作用。 即使像你这样的东西是愚蠢的使用指针而不是这将是伟大的。我只是试图想象它是否可能这样!!

#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;
}

7 个答案:

答案 0 :(得分:2)

int i; for(i=0;i<d;i++){ if(a[i] > '9' || a[i]< '0') { printf("%c",a[i]); } } 函数中,只需输入以下代码 - &gt;&gt;

即可
Sub zipAllFiles()
 'rondebruin <--- Credits to code

Dim FileNameZip, FolderName
Dim strDate As String, DefPath As String
Dim oApp As Object
Dim Fold As Range
Dim ws As Worksheet
Dim i As Integer, lastrow As Long

DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
    DefPath = DefPath & "\"
End If

Set ws = ThisWorkbook.Sheets(2)
lastrow = ws.Cells(Rows.Count, 5).End(xlUp).Row  '---> Files Directories

strDate = Format(Now, " dd-mmm-yy h-mm-ss")
FileNameZip = DefPath & "MyFilesZip " & strDate & ".zip"


'Create empty Zip File
NewZip (FileNameZip)

'E3:E&lastrow  ---> Where the files directories are located.
For Each Fold In ws.Range("E3:E" & lastrow)
    Set oApp = CreateObject("Shell.Application")
    FolderName = Fold.Value
    'Copy the files to the compressed folder
    oApp.Namespace(FileNameZip).CopyHere FolderName
Next Fold


ws.Range("J1").Value = Dir(FileNameZip) '---> The directory of the Zipped file to Range(J1).

 Application.DisplayAlerts = False


 End Sub

上面的代码将打印除Numbers(0-9)以外的所有字符。如果你想删除特殊字符,那么请参考特殊字符的ASCII值并在代码中提及

答案 1 :(得分:1)

您可以使用isalpha检查字母字符

#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;
}

答案 2 :(得分:0)

最简单的方法是在输入阶段跳过数字:

        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++;
                }
        }

答案 3 :(得分:0)

如果您只想打印字符,则无需从阵列中删除它们。只需打印非整数的那些。

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条件,数组将只包含字符。然后,您可以正常打印。

答案 4 :(得分:0)

你可以简单地做

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

答案 5 :(得分: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;
}

答案 6 :(得分:0)

所有答案都给了我想要的东西。感谢所有不同的方法并帮助我学习! @Klas Lindback,@ samarth kejriwal,@ myxaxa,@ BLUEPIXY,@ LPs,@ Rishikesh Raje