代码之家  ›  专栏  ›  技术社区  ›  Ashutosh

修剪int字符串

  •  1
  • Ashutosh  · 技术社区  · 14 年前

    在我的程序中,我想输入一个整数字符串,例如2107900000。我想得到它的长度,然后想删除所有的0和1。

    3 回复  |  直到 14 年前
        1
  •  1
  •   JoshD    14 年前

     char buffer[20];
     char num[] = "2107900000";
     int j = 0;
     for (int i = 0; num[i]; i++)
     {
       if (num[i] != '0' && num[i] != '1')
       {
         buffer[j] = num[i];
         j++;
       }
     }
     buffer[j] = 0 //null terminator
    

     char num[] = "2107900000";
     int j = 0;
     for (int i = 0; num[i]; i++)
     {
       if (num[i] != '0' && num[i] != '1')
       {
         num[j] = num[i];
         j++;
       }
     }
     num[j] = 0 //null terminator
    
        2
  •  2
  •   Mark Elliot    14 年前

    stripped orig

    void strip_0s_and_1s(char *orig, char *stripped)
    {
        // while we haven't seen a null terminator
        while(*orig){
            // if the current character is not a 1 or a 0...
            if(*orig != '0' && *orig != '1'){
               // copy the character
               *stripped= *orig;
               stripped++;
            }
            // increment pointer to old string
            orig++;
        }
        // terminate 'stripped' string
        *stripped= '\0';
    }
    

    #include <stdio.h>
    #include <string.h>
    int main (int argc, char **argv) {
        int len; 
        char str[100]; // max string length of 99+1 null terminator
        scanf("%s", str); 
        len = strlen(str);
        printf("%d", len); 
        return 0; 
    }
    

    new

        3
  •  1
  •   R.. GitHub STOP HELPING ICE    14 年前

    for (i=j=0; s[i]=s[j]; i+=((unsigned)s[j++]-'0'>2));

    i j j++ (unsigned)s[j++]-'0'>2 '0' '1'