Home > Software design >  How to omit the space after the comma when scanning comma separated strings in C
How to omit the space after the comma when scanning comma separated strings in C

Time:12-29

When inputting strings which are separated by a comma the second string will print with space on the beginning. I need to scan and print comma separated string without whitespace in the beginning. This is my current code.

#include <stdio.h>
int main()
{
    char s[30];
    char r[30];
  
    scanf("%[^,],%[^\n]",s,r);
    
      printf("%s\n",s);
      printf("%s",r);

  
    return 0;
}

The output when hello world, o wor is the input is

hello world
 o wor

It should be

hello world 
o wor

CodePudding user response:

Adding a whitespace before %[] will exclude it.

scanf("%[^,], %[^\n]",s,r);
  • Related