edmung vunxh
edmung vunxh

Reputation: 13

How to omit the space after the comma when scanning comma separated strings in C

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

Upvotes: 0

Views: 45

Answers (1)

opsJson
opsJson

Reputation: 36

Adding a whitespace before %[] will exclude it.

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

Upvotes: 2

Related Questions