2

「行」でマークされた行を理解するのが難しい:-

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<ctype.h>

int main(void)
{
char s[81], word[81];
int n= 0, idx= 0;

puts("Please write a sentence:");
fgets(s, 81, stdin);
while ( sscanf(&s[idx], "%s%n", word, &n) > 0 )    //line
{
    idx += n;
    puts(word);
}

return 0;
}

「行」でマークされた行を次の行に置き換えることはできますか:

while ( sscanf(&s[idx], "%s%n", word, &n) )
4

5 に答える 5

0

ここを見てください:sscanfの説明

標準入力から 80 文字を取得し、それらを char[] s に格納してから、一度に 1 語ずつ出力します。

while ( sscanf(&s[idx], "%s%n", word, &n) > 0 ) //copy from "s" into "word" until space occurs
//n will be set to position of the space
//loop will iterate moving through "s" until no matching terms found or end of char array
于 2013-09-23T13:24:09.440 に答える
0

ここに小さな翻訳があります:

int words_read;
while (1) {

    // scscanf reads with this format one word at a time from the target buffer
    words_read = sscanf(
          &s[idx] // address of the buffer s + amount of bytes already read
        , "%s%n" // read one word
        , word // into this buffer
        , &n // save the amount bytes consumed inbto n
        );


    if (words_read <= 0) // if no words read or error then end loop
        break;

    idx += n; // add the amount of newlyt consumed bytes to idx

    puts(word); // print the word
} 
于 2013-09-23T13:22:20.063 に答える