从先前数组的子集创建新数组

Creating a new array from a subset of a previous array

我有一个这样的字符数组:

    #define SIZE 4096
    char array[SIZE];
    fgets(array, SIZE, stdin);

我想删除任何标点符号,例如 ('),这样数组元素将是:

做点什么

相对于

做这件事

我将如何制作一个不包含标点符号的新数组?

只需复制新数组,避免标点符号,并在输出数组末尾添加一个 null 终止符:

int main() 
{
    char punctuation[] = { '.', '?', '!', ':', ';',
                            '-', '(', ')', '[', ']',
                            ',', '"', '/'};

    char input[SIZE], output[SIZE];
    int i, j, k, ch, flag;

    i = j = k = flag = 0;

    /* get the input string from the user */
    printf("Enter your input string:");
    fgets(input, SIZE, stdin);
    input[strlen(input) - 1] = '[=10=]';

    /* copy characters other than punctuations */
    while (input[i] != '[=10=]') 
    {
        flag = 0;
        ch = input[i];

        for (j = 0; j < sizeof(punctuation); j++) 
        {
            if (ch == punctuation[j])
            {
                flag = 1;
                break;
            }
        }

        if (!flag) 
        {
            output[k++] = input[i];
        }

        i++;
    }

    output[k] = '[=10=]';

    /* print the resultant string */
    printf("Resultant String: %s\n", output);
    return 0;
}

您可以根据需要 increase/decrease punctuation 数组大小。默认的 C 语言环境将这些字符分类为标点符号:

!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

在 C 中还有一个默认函数 ispunct

int main()
{
    int i;
    printf("All punctuation characters in C"
            " programming are: \n");
    for (i = 0; i <= 255; ++i)
        if (ispunct(i) != 0)
            printf("%c ", i);
    return 0;
}
#define SIZE 4096
char array[SIZE];

char result[SIZE];
fgets(array, SIZE, stdin);

for(int i=0; array[i]; i++)
{
    char a;
    a = array[i];
    if((a > 0x20) && (a < 0x2f))
        continue;
    else
        result[i] = a;
}

result[i] = '/0';