0

我想获取一个包含字母和字符的字符串,并只过滤掉字母。然后我想重用该字符串并将其放入一个数组中。我将如何在 C 中做到这一点?
我使用过isalpha(),但只使用 into printf,而不是变量。谢谢你的帮助。

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

int main(void)
{
    int i;
    string w = "name1234";
    string new ="";
    int length = strlen(w);

    for (i=0; length > i; i++)
    {
        if (isalpha(w[i]))
        {
            new = w;
        }
    }
    printf("This is the new one: %s\n", new);  //it should be 'name' not 'name1234'
    return 0;
}
4

5 回答 5

4
#include <stdio.h> 
#include <string.h>  
#include <ctype.h>

int main(void)
{
    int i, newx;
    char w[] = "name1234";

    int length = strlen(w);

    // in C99 or greater, you can do
    // char new[length+1];
    // to get a variable-length local array, instead of using malloc
    char* new = malloc(length+1);
    if (!new)
    {
         fprintf(stderr, "out of memory\n");
         return 1;
    }

    for (i = 0, newx = 0; length > i; i++)
    {
        if (isalpha((unsigned char)w[i]))
        {
            new[newx++] = w[i];
        }
    }
    new[newx] = '\0';

    printf("This is the new one: %s\n", new);  //it should be 'name' not 'name1234'
    free(new); // this isn't necessary since all memory is freed when the program exits
               // (and it isn't appropriate if new is a local array) 
    return 0;
}
于 2012-11-10T21:01:12.313 回答
2

这是一个简单的代码。确保你理解每一行。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char org[]="abc123defg";
    int size=sizeof(org)/sizeof(org[0]);   //size will hold the original array length 
    char* res=malloc((sizeof(char)*size)+1);  //+1 for the '\0' char that indicates end of a string
    if(!res)
        return 1; //allocation failed
    int i; int k=0;
    for(i=0;i<size;i++)
    {
        if(isalpha(org[i])) {
            res[k]=org[i];
            k++;
        }
    }
    res[k]='\0';
    printf("%s\n", res);  //now it will be abcdefg
    free(res);  //free the memory I've allocated for the result array
    return 0;
}
于 2012-11-10T21:06:30.293 回答
1

定义正确的数据类型 char * 而不是字符串。您想过滤字符串 A 并将结果放入 B,您必须:

1) Create char *a[size], char *b[size];
2) iterate over the "string" A and verify if the actual position (char) meets the requirements to go to the "string" B;
3) if it does, just do b[pos_b] = a[i]; and increment the variable pos_b.

您必须声明 pos_b 因为您在数组 b 中的位置可能与您在数组 A 中的位置不同。因为,您只是在数组 b 中添加字母。

于 2012-11-10T21:01:57.393 回答
1

这是学术版:

const char* w= "name1234";
char* filteredString= (char*)malloc(sizeof(char));
const long length=strlen(w);
long size=0;
for(long i=0; i<length; i++)
{
    if(isalpha(w[i]))
    {
        size++;
        filteredString=(char*)realloc(filteredString, (size+1)*sizeof(char));
        filteredString[size-1]=w[i];
    }
}
filteredString[size]=0;
puts(filteredString);

只是为了学习使用 realloc,但您可以在堆栈上分配整个字符串并拧紧内存使用情况,realloc 占用 CPU。

于 2012-11-10T21:17:48.033 回答
0

好吧,我对您的代码进行了一些小修改以使其正常工作,但是您可以使用一些库,例如string.h. 我希望你明白:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
    int i, j = 0;
    /* I limited your strings to avoid some problems. */
    char w[20]  = "name1234";
    /* The name "new" is a reserved word in C (at least in my IDE). */
    char nw[20] = "";
    int length  = strlen(w);
    for(i = 0; i < length; i++)
    {
        /* I added the special string end character '\0'. */
        if(isalpha(w[ i ]) || w[ i ] == '\0')
        {
            nw[ j ] = w[ i ];
            j++;
        }
    }
    printf("This is the new one: %s\n", nw);
    return 0;
}
于 2012-11-10T21:05:09.880 回答