代码之家  ›  专栏  ›  技术社区  ›  Mattia Surricchio

C-忽略scanf()中的空格

  •  2
  • Mattia Surricchio  · 技术社区  · 6 年前

    我想做一个简单的字符串采集。我需要的是从输入(stdin)中编写一个字符串,它可以包含空格,并保存它 没有 单词之间的任何空格。

    到目前为止,我已经编写了这个简单的代码,它可以保存所有内容(也包括空格),但是我不知道如何使 scanf() 忽略空格。

    int main(){
        char str[10];
        scanf("%[^\n]s, str);
        printf("%s", str;
    }
    

    例如 :

    如果输入是: I love C programming! 我的输出应该是: IloveCprogramming!

    我试着用 %* ,用于忽略字符,但没有任何成功。

    我也知道我可以“重新扫描”字符串一旦被保存和删除所有的空间,但我需要尽可能高效地进行采集,重新扫描每个字符串以删除空间将大大增加计算时间(而不仅仅是扫描和忽略,其复杂性为o(n))。

    5 回复  |  直到 6 年前
        1
  •  1
  •   John Bollinger    6 年前

    scanf()

    scanf %s %[

    %[^\n]

    int main(void) {
        int field_count;
    
        do {
            char str[80];
            char tail;
    
            field_count = scanf("%79[^ \n]%c", str, &tail));
            if (field_count == 0) {
                // No string was scanned this iteration: the first available char
                // was a space or newline.  Consume it, then proceed appropriately.
                field_count = scanf("%c", &tail);
                if (field_count != 1 || tail == '\n') {
                    // newline, end-of-file, or error: break out of the loop
                    break;
                } // else it's a space -- ignore it
            } else if (field_count > 0) {
                // A string was scanned; print it:
                printf("%s", str);
    
                if (field_count == 2) {
                    // A trailing character was scanned, too; take appropriate action:
                    if (tail == '\n') {
                        break;
                    } else if (tail != ' ') {
                        putchar(tail);
                    } // else it is a space; ignore it
                }
            } // else field_count == EOF
        } while (field_count != EOF);
    }
    

    • %79[^ \n]
    • [ s
    • %c
    • ' '
        2
  •  4
  •   Ed Heal    6 年前

    getc

    int ch;
    char str[10];
    
    // Loop until either loop reaches 9 (need one for null character) or EOF is reached
    for (int loop = 0; loop < 9 && (ch = getc(stdin)) != EOF; ) {
       if (ch != ' ' ) {
         str[loop] = ch;
         ++loop;
       }
    }
    str[loop] = 0;
    
    printf("%s", str);
    

        3
  •  2
  •   ryyker    6 年前
    isspace(.)






    isspace(.);

    int main(void)
    {
        char string[] = {"this contain's \n whitespace\t"};
        int len = strlen(string);
        char out[len+1];// +1 for null terminator 
                        //(accommodates when input contains no whitespace)
        int count = clean_whitespace(string, out);
    
        return 0;
    }
    
    int clean_whitespace(const char *in, char *out)
    {
        int len, count=0, i;
        if((in) && (out))
        {
            len = strlen(in);
            for(i=0;i<len;i++)
            {
                if(!isspace(in[i]))
                {
                    out[count++] = in[i];
                }
            }
            out[count]=0;//add null terminator.
        }
        return count;
    }
    
        4
  •  2
  •   chqrlie    6 年前

    scanf()

    #include <stdio.h>
    
    int main(void) {
        int c;
        while ((c = getchar()) != EOF) {
            if (c != ' ') {
                putchar(c);
            }
            if (c == '\n') {
                break;
            }
        }
        return 0;
    }
    

    • s %[^\n]
    • scanf("%9[^\n]", str);
    • printf

    char c; while (scanf(" %c", &c) == 1) { putchar(c); }

        5
  •  0
  •   Batıkan Bora Ormancı    6 年前

     int main() {
       char a[10];
        for(int i = 0; i < 10 ; i++){
            scanf("%c", &a[i]);
            if( a[i] == ' ')
            i--;
        }
    }
    

        for(int i = 0; i < 9; i++){
           printf("%c,", a[i]);
         }
    
        printf("%c", a[9]);