无法用空格扫描字符串

时间:2022-01-28 19:54:42

https://www.codechef.com/problems/LADDU not able to scan string in the array "work" on line 12 of the code.

https://www.codechef.com/problems/LADDU无法扫描代码第12行的数组“work”中的字符串。

     #include<stdio.h>

     int main()
     {
        long long int i,j,T,actv,points,a,b,c;
        char origin[100],work[100];
        scanf("%lld",&T);
        while(T--)
        {
            points=0;
            scanf("%lld %s",&actv,origin);
            for(i=0;i<actv;i++)
            {
                    printf("hie\n");
                    scanf("%[^\n]s",work);
                    printf("hello\n");
            }
       }

       return 0;
    }

4 个解决方案

#1


2  

Instead of scanf() use fgets() to scan a string with spaces.

而不是scanf()使用fgets()来扫描带空格的字符串。

fgets(work,sizeof(work),stdin);

Note: fgets() comes with a newline character. So

注意:fgets()带有换行符。所以

size_t n = strlen(work);
if(n>0 && work[n-1] == '\n')
{
   work[n-1] = '\0';
}

#2


1  

Use fgets instead of scanf.

使用fgets而不是scanf。

#define BUFFERSIZE sizeof(work)

if (fgets(work, BUFFERSIZE , stdin) != NULL)
{
   // your stuff
}

To get your string with spaces.

获取带空格的字符串。

If your C-stirng is shorter than BUFFERSIZE last char before null terminator will be '\n'

如果你的C-stirng比BUFFERSIZE更短,那么在null终结符之前将是'\ n'

#3


0  

scanf("%lld %s",&actv,origin);

After the line is executed, newline remains unconsumed.

执行该行后,换行仍然没有消耗。

scanf("%[^\n]s",work);

Since %[^\n]doesn't accept the newline, it doesn't perform read.
(Also, s is not required. E.g. %[^\n]s --> %[^\n])

由于%[^ \ n]不接受换行符,因此它不执行读取操作。 (此外,s不是必需的。例如,%[^ \ n] s - >%[^ \ n])

So, Change such in the following.

所以,改变如下。

scanf("%lld %s%*c",&actv,origin);//%*c for skip one character(newline) or while(getchar()!='\n');//skip upto newline
...
scanf("%99[^\n]%*c",work);

scanf(“%lld%s%* c”,&actv,origin); //%* c用于跳过一个字符(换行符)或while(getchar()!='\ n'); //跳过换行符... .scanf(“%99 [^ \ n]%* c”,工作);

#4


0  

for fast reading/writing of numbers use:

快速阅读/写入数字使用:

#include <stdio.h>

void fastRead( size_t *a );
void fastWrite( size_t a );

inline void fastRead(size_t *a)
{
    int c=0;
    // note: 32 is space character
    // consume leading trash
    while (c<33) c=getchar_unlocked();

    // initialize result value
    *a=0;

    // punctuation parens, etc are show stoppers
    while (c>47 && c <58)
    { // then in range 0...9
        *a = (*a)*10 + (size_t)(c-48);
        c=getchar_unlocked();
    }
    //printf( "%s, value: %lu\n", __func__, *a );
} // end function: fastRead


inline void fastWrite(size_t a)
{
    char snum[20];
    //printf( "%s, %lu\n", __func__, a );

    int i=0;
    // decompose 'a' into char array
    do
    {
        // 48 is numeric character 0
        snum[i++] = (char)((a%10)+(size_t)48);
        a=a/10;
    }while(a>0);

    i=i-1; // correction for overincrement from prior 'while' loop

    while(i>=0)
    {
        putchar_unlocked(snum[i--]);
    }
    putchar_unlocked('\n');
} // end function: fastWrite

Along with the above two functions, here is a typical program using those functions:

除上述两个函数外,这里还有一个使用这些函数的典型程序:

#define MAX_VALUE (1000000)

int array[ MAX_VALUE +1 ];

int main( void )
{
    // get number of test cases
    size_t  numTestCases;
    fastRead( &numTestCase );
    //scanf( "%lu", &numTestCases );
    //printf( "%s, Number of Test Cases: %lu\n", __func__, numTestCases);

    // accumulate test cases, sorted
    for( size_t i=0; i<numTestCases; i++ )
    {
        size_t value;
        fastRead( &value );
        //scanf( "%lu", &value );
        array[value]++;
    }

    // output the unique values, assending
    for( size_t i=0; i<MAX_VALUE; i++ )
    {
        if( array[i] )
        {
            fastWrite( i );
            //printf( "%s. %lu\n", __func__, i );
        }
    }


    return 0;
}

The fastRead and fastWrite functions (rather than printf and scanf) will greatly speed up your code.

fastRead和fastWrite函数(而不是printf和scanf)将大大加快您的代码速度。

Now you only need to implement the problem set.

现在您只需要实现问题集。

Note: to read in a string:

注意:要读入一个字符串:

size_t i = 0;
while( i < sizeof( inputArray ) && (ch = getchar_unlocked()) && ' ' != ch )
{
    inputArray[i] = ch;
    i++;
}
inputArray[i] = '\0';

You could use some other string delimiter, like a '\n', rather than ' '

您可以使用其他字符串分隔符,例如'\ n',而不是''

#1


2  

Instead of scanf() use fgets() to scan a string with spaces.

而不是scanf()使用fgets()来扫描带空格的字符串。

fgets(work,sizeof(work),stdin);

Note: fgets() comes with a newline character. So

注意:fgets()带有换行符。所以

size_t n = strlen(work);
if(n>0 && work[n-1] == '\n')
{
   work[n-1] = '\0';
}

#2


1  

Use fgets instead of scanf.

使用fgets而不是scanf。

#define BUFFERSIZE sizeof(work)

if (fgets(work, BUFFERSIZE , stdin) != NULL)
{
   // your stuff
}

To get your string with spaces.

获取带空格的字符串。

If your C-stirng is shorter than BUFFERSIZE last char before null terminator will be '\n'

如果你的C-stirng比BUFFERSIZE更短,那么在null终结符之前将是'\ n'

#3


0  

scanf("%lld %s",&actv,origin);

After the line is executed, newline remains unconsumed.

执行该行后,换行仍然没有消耗。

scanf("%[^\n]s",work);

Since %[^\n]doesn't accept the newline, it doesn't perform read.
(Also, s is not required. E.g. %[^\n]s --> %[^\n])

由于%[^ \ n]不接受换行符,因此它不执行读取操作。 (此外,s不是必需的。例如,%[^ \ n] s - >%[^ \ n])

So, Change such in the following.

所以,改变如下。

scanf("%lld %s%*c",&actv,origin);//%*c for skip one character(newline) or while(getchar()!='\n');//skip upto newline
...
scanf("%99[^\n]%*c",work);

scanf(“%lld%s%* c”,&actv,origin); //%* c用于跳过一个字符(换行符)或while(getchar()!='\ n'); //跳过换行符... .scanf(“%99 [^ \ n]%* c”,工作);

#4


0  

for fast reading/writing of numbers use:

快速阅读/写入数字使用:

#include <stdio.h>

void fastRead( size_t *a );
void fastWrite( size_t a );

inline void fastRead(size_t *a)
{
    int c=0;
    // note: 32 is space character
    // consume leading trash
    while (c<33) c=getchar_unlocked();

    // initialize result value
    *a=0;

    // punctuation parens, etc are show stoppers
    while (c>47 && c <58)
    { // then in range 0...9
        *a = (*a)*10 + (size_t)(c-48);
        c=getchar_unlocked();
    }
    //printf( "%s, value: %lu\n", __func__, *a );
} // end function: fastRead


inline void fastWrite(size_t a)
{
    char snum[20];
    //printf( "%s, %lu\n", __func__, a );

    int i=0;
    // decompose 'a' into char array
    do
    {
        // 48 is numeric character 0
        snum[i++] = (char)((a%10)+(size_t)48);
        a=a/10;
    }while(a>0);

    i=i-1; // correction for overincrement from prior 'while' loop

    while(i>=0)
    {
        putchar_unlocked(snum[i--]);
    }
    putchar_unlocked('\n');
} // end function: fastWrite

Along with the above two functions, here is a typical program using those functions:

除上述两个函数外,这里还有一个使用这些函数的典型程序:

#define MAX_VALUE (1000000)

int array[ MAX_VALUE +1 ];

int main( void )
{
    // get number of test cases
    size_t  numTestCases;
    fastRead( &numTestCase );
    //scanf( "%lu", &numTestCases );
    //printf( "%s, Number of Test Cases: %lu\n", __func__, numTestCases);

    // accumulate test cases, sorted
    for( size_t i=0; i<numTestCases; i++ )
    {
        size_t value;
        fastRead( &value );
        //scanf( "%lu", &value );
        array[value]++;
    }

    // output the unique values, assending
    for( size_t i=0; i<MAX_VALUE; i++ )
    {
        if( array[i] )
        {
            fastWrite( i );
            //printf( "%s. %lu\n", __func__, i );
        }
    }


    return 0;
}

The fastRead and fastWrite functions (rather than printf and scanf) will greatly speed up your code.

fastRead和fastWrite函数(而不是printf和scanf)将大大加快您的代码速度。

Now you only need to implement the problem set.

现在您只需要实现问题集。

Note: to read in a string:

注意:要读入一个字符串:

size_t i = 0;
while( i < sizeof( inputArray ) && (ch = getchar_unlocked()) && ' ' != ch )
{
    inputArray[i] = ch;
    i++;
}
inputArray[i] = '\0';

You could use some other string delimiter, like a '\n', rather than ' '

您可以使用其他字符串分隔符,例如'\ n',而不是''