Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9076235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:04:00+00:00 2026-06-16T19:04:00+00:00

Is there a way to control how scanf() separates input? scanf(%10s %10s, array1, array2);

  • 0

Is there a way to control how scanf() separates input?

scanf("%10s %10s", array1, array2);

The above works fine, separating by space. But, can I use a different separation signal?

Because neither of these seem to work if I want to splice input with a comma rather than a space:

scanf("%10s,%10s", array1, array2);
scanf("%10s, %10s", array1, array2);

What gives? A book I am reading claims that one can separate input with scanf() using any character, but fails to explain how to do so. Furthermore, if alternate characters can be used, can a broader criteria than a single character be used, too? (i.e. value types, statements, etc.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:04:01+00:00Added an answer on June 16, 2026 at 7:04 pm

    Using character classes:

    #include <stdio.h>
    
    int main(int argc, const char **argv) {
      char s1[10], s2[10];
      const char *str = "word1,word2";
      sscanf(str, "%[^,],%s", s1, s2);
      printf("%s -- %s\n", s1, s2);
      return 0;
    }
    

    Or you can be even more specific:

    sscanf(str, "%[^,],%[^,]", s1, s2);
    

    which will also capture white space in s2

    To split on multiple characters you can use strstr:

    #include <stdio.h>
    #include <string.h>
    int main(int argc, const char **argv) {
      const char *str = "word1fooword2fooword3", *foo = "foo", *ptr;
      const char *eofstr = str;
      for (ptr = str; eofstr; ptr = eofstr + strlen(foo)) {
        char word[10];
        eofstr = strstr(ptr, foo);
        if (eofstr) {
          size_t len = eofstr - ptr;
          strncpy(word, ptr, len);
          word[len] = 0;
          printf("%s\n", word);
        } else {
          strcpy(word, ptr);
          printf("%s\n", word);
        }   
      }
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better way to control the space between certain block elements.. i
Is there a way to control the vertical space between two HTML paragraphs, in
Is there a way to control what space of the screen windows can be
Is there a way to control how .Net sets the Name and ID property
Is there a way to control the Maven embedded Tomcat version? mvn tomcat:run This
Is there any way to control and manual specification for start of teaser ?.
Is there a way to control groovy's MarkupBuilder's output and filter out the newline
Is there way to mute an audio stream or at least control the volume?
Is there a way to put the same control in different tabs using C#?
Is there a way to extend the SPGridView control in a webpart such that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.