【问题标题】:Selective removal of specific char in a string选择性删除字符串中的特定字符
【发布时间】:2015-02-19 02:13:49
【问题描述】:

假设我有一个看起来像这样的字符串:

  "value" "some other value"   "other value"  "some value"     

我的目标是有选择地删除空白,如下所示:

"value""some other value""other value""some value"

使得空格保留在引号内的字符串中:

"some other value"  

我有以下功能:

void rmChar(char *str, char c)
{
char *src, *dest;
src = dest = str; 

while(*src != '\0') 
{
    if (*src != c)   
    {
        *dest = *src;  
        dest++;        
    }
    src++;         
}
*dest = '\0';        
}

它删除了 str 中所有出现的 char c ,但我应该使用更多的条件表达式来仅在某些事情发生时才进行删除。

有什么线索吗?

【问题讨论】:

    标签: c arrays string text char


    【解决方案1】:

    我只是想这样做。下面是我的程序。

    注意:这可能不是一个高效的程序(时间或空间复杂性不好),但是它可以完成您正在尝试做的事情(如果我理解您的问题的话)。

    注意 另外我在这段代码中使用了 malloc()。如果您在不使用任何其他字符串的情况下更改原始字符串的内容,则不会使用它。但正如我从您的问题中了解到的那样,您正在制作一个新字符串,其中包含删除空格后原始字符串的值。

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    void rmChar(char *,char, int );
    int main()
    {
        char string[200] = "\"This is a value\"  \"and another value\"   \"value2 this\"";
        char c;
        c = '"';
        printf("%s\n",string);
        int len = strlen(string);
        /*Pass the address of the stringi, char c, and the length of the string*/
        /*Length of the string will be required for malloc() inside function rmChar()*/
        rmChar(string, c, len);
    
        return 0;
    }
    
    void rmChar(char *str,char c, int len)
    {
        char *dest1, *dest2;
        char *src = str;
    
        int removeFlag = 0; /* You will remove all the spaces ' ' that come after removeFlag is odd*/
    
        dest1 = malloc(len);
        dest2 = dest1;
    
        while(*str != '\0')
        {
                if(*str == c)
                {
                        removeFlag++;
                        if (removeFlag %2 == 0)
                        {
                                /* This is required because every 2nd time you get a " removeFlag is increased so next if is NOT true*/
                                *dest2 = *str;
                                dest2++;
                        }
                }
                if ((removeFlag % 2) == 1)
                {
                        *dest2 = *str;
                        dest2++;
                }
                str++;
        }
        *dest2 = '\0';
        printf("%s\n", dest1);
        /* If you want to copy the string without spaces to the original string uncomment below line*/
    
        //strcpy(src, dest1);
    
        free(dest1);
    }
    

    您还需要一个变量来用作某种标志,该标志表示“之后您需要删除空格。然后您会以某种方式在if() 语句中使用该标志。这里int removeFlag 是我的标志用过。

    【讨论】:

    • 谢谢您,好先生!我已经设法使用奇偶校验(就像你做的那样)和一些更多的测试(对于更复杂的字符串)来做到这一点。
    • @AlexandruDinu 很高兴它有帮助。是的,我想有很多方法可以做到这一点。如果您探索所有这些,那就太好了,这样您就可以使用最好的方法(使用最少的变量或最少的代码行)。
    【解决方案2】:

    遍历字符串的循环必须跟踪它当前是否正在查看带引号的字符串中的字符,然后使用该信息仅在适当时删除。

    要跟踪该信息,您可以使用一个额外的变量,每次有 " 时都会更新该变量。

    int quoted = 0;
    
    while (...) {
       if (*src == '"') {
         // set `quoted` to 1 or 0, as appropriate
         ...
       }
    
       // delete only if !quoted
       ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      相关资源
      最近更新 更多