繁体   English   中英

从字符串数组中的字符串中删除空格

[英]removing spaces from a string in an array of strings

我目前正在研究一个从输入文件中读取字符串并将其存储到数组中的项目。 当将其存储到数组中时,我想删除空格,以便可以将数组中的字符串与数组字符串卡进行比较,并检查输入文件中的所有Cards是否都在那里。

但是我目前仍然坚持在数组中没有空格的情况下存储新字符串并打印出来。 它打印出第一个字符串REDA但是之后我得到了Segmentation Fault。

如果有人可以给我一些如何将cardarray中的字符串与常量数组进行比较,并检查所有card是否都在数组中的提示,我也将不胜感激。

我希望这是正确的方法。

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

const char * stringcard[] = { "REDA","RED2"
                              "RED3"
                              "RED4"
                              "RED5"
                              "RED6"
                              "RED7"
                              "RED8"
                              "RED9"
                              "RED10"
                              "REDJ"
                              "REDQ"
                              "REDK"
                            };

int main (int argc, char **argv) {

    char *reds[13];
    char * cardarray[13];

    int i;

    FILE *file = argc > 1 ? fopen (argv[1], "r") : stdin;
    if (file == NULL)
        return 1;
    if(argc!=2) {
        printf("[ERR]");
        return 0;
    }

    for (i =0; i < 13; i++) {

        reds[i] = malloc( 8);
        fgets(reds[i], 8, file);

    }

    int i2 = 0;
    for (i =0; i < 13; i++) {

        printf ("%s", reds[i]);

    }

    for(i= 0; i<13; i++) {
        char *p = strtok (reds[i], " ");


        while (p != NULL)
        {
            cardarray[i2++] = p;
            p = strtok (NULL, " ");
        }
    }

    for (i =0; i < 13; i++) {

        printf ("%s", cardarray[i]);

    }


    return 0;
}

输入文件:

RED A
RED 2
RED 3
RED 4
RED 5
RED 6
RED 7
RED 8
RED 9
RED 10
RED J
RED Q
RED K

只需删除不需要的字符。 这里有两个功能:

第一种算法要快得多。 第二慢但容易理解

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

char *removechar(char *str, int ch)
{
    char *cptr = str, *readptr = str;

    while(*readptr)
    {
        if(*readptr == ch)
        {
            readptr++;
        }
        else
        {
            *cptr++ = *readptr++;
        }
    }
    *cptr = 0;
    return str;
}

char *removechar(char *str, int ch)
{
    char *cpos = str;

    while((cpos = strchr(cpos, ch)))
    {
        strcpy(cpos, cpos + 1);
    }
    return str;
}

至少有两个问题:

第一:在此循环中,您将i递增两次,最终导致缓冲区溢出。

for (i = 0; i < 13; i++) {
    char *p = strtok(reds[i], " ");    

    while (p != NULL)
    {
      if (i >= 13)           // debug code
      {                      // debug code
        printf("Bummer\n");  // debug code
        exit(1);             // debug code
      }                      // debug code
      cardarray[i++] = p;
      p = strtok(NULL, " ");
    }
  }

其次:您没有在此处分配足够的内存:

reds[i] = malloc(sizeof(char) * (4 + 1));   // you allocate space for 5 chars
fgets(reds[i], 13, file);                   // and here you tell fgets that
                                            // your buffer has a length of 13 chars...

不过,最有可能出现更多错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM