Is there a way to read a c-string and then an int with one scanf in C?

Hey, I'm trying to get this function to get the following output with the specified input, "..." is where I am not sure what to write:

void Question8(void)
{
  char sentence[100];    
  int grade;    
  scanf(….);    
  printf("%s %d", sentence, grade);    
}

Input:
My CS Grade is 1000

Output:
My CS Grade is 100

However, the kicker is what I need for scanf to read the c-string and then int with a single scanf command, is this possible?

Edit: I can only edit the code in a location with three periods ("..."), I can’t use anything else. I can assume that an input list is expected, but I can’t change anything beyond three periods. The output does not contain typos; the purpose of this assignment is to use flags and escape sequences.

+5
source share
4

Visual Studio 2008

#include <stdio.h>

    int main()
    {
      char sentence[100];    
      int grade = 0;    
      scanf("%[^0-9] %d",sentence,&grade);
      printf("%s %d", sentence, grade);   
      return 1;
    }


Input : 
My CS Grade is 100
Output :
My CS Grade is 100
+1

:

<obligatory_rant>
    stupid question, but I guess it homework and you're
    stuck with these absurd limitations
</obligatory_rant>

, , , :

if (scanf("%100[^0-9] %3d", text, &number) == 2)
    ...

:

  • 100 "%100[... .
  • %3d, , 1000 100.
  • [^...] , not ( "^" ) , 0-9 - .
  • if (... == 2) , / .

if , :

scanf("%100[^0-9] %3d", text, &number)
+2

scanf, . , , . :

  char sentence[100];
  int grade;
  scanf("%[^0-9] %d",sentence,&grade);
  printf("%s %d\n", sentence, grade);
+2

. scanf "%14c%3d", sentence, &grade

printf, . , ( ), . 1000 100.

, . , . , .

0

All Articles