{"id":2228,"date":"2024-05-10T07:15:46","date_gmt":"2024-05-10T07:15:46","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2228"},"modified":"2024-05-10T07:15:46","modified_gmt":"2024-05-10T07:15:46","slug":"scansets-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/scansets-in-c\/","title":{"rendered":"Scansets In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#scansets-in-c\">Scansets In C<\/a><\/li><li ><a href=\"#simple-scanset-example\">Simple Scanset Example <\/a><\/li><li ><a href=\"#another-scanset-example-with\"> Another scanset example with ^<\/a><\/li><li ><a href=\"#implementation-of-gets-function-using-scanset\"> Implementation of gets() function using scanset<\/a><\/li><li ><a href=\"#faq-scansets-in-c\">FAQ- Scansets In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scansets-in-c\">Scansets In C<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Scanset specifiers are used to specify a set of characters that Scanf should match and store in a variable. In C&#8217;s scanf family of functions, scanset specifiers are denoted by %[]. Inside a scanset, you can specify either single characters or a range of characters that you want to match and store. It&#8217;s important to note that scansets are case-sensitive, meaning uppercase and lowercase characters are treated as distinct. You can define a scanset by placing characters inside square brackets. You can also use a comma to separate individual characters or character ranges within the scanset.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"simple-scanset-example\">Simple Scanset Example <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The below example will store only capital letters to the character array \u2018str\u2019, any other character will not be stored inside the character array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* A simple scanset example *\/\n#include &lt;stdio.h&gt;\n \nint main(void)\n{\n    char str&#91;128];\n \n    printf(\"Enter a string: \");\n    scanf(\"%&#91;A-Z]s\", str);\n \n    printf(\"You entered: %s\\n\", str);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@centos-6 C]# .\/scan-set \n  Enter a string: Skill Vertex\n  You entered: Skill Vertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"another-scanset-example-with\"> Another scanset example with ^<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In C programming, when you use scanset specifiers in functions like scanf, you can put characters inside square brackets to specify a set of characters you want to read. If you start with a &#8216;^&#8217; inside the brackets, it means you want to read characters that are NOT in the specified set. The scanning will stop as soon as it finds a character that is part of the specified set. For example, if you specify &#8220;%[^o]s&#8221; and input &#8220;Skill Vertex,&#8221; it will read and store characters until it finds the letter &#8216;o,&#8217; and then it stops. So, in this case, it will store &#8220;Skill&#8221; in your variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>scanf(\"%&#91;^o]s\", str);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Look at the example given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Another scanset example with ^ *\/\n#include &lt;stdio.h&gt;\n \nint main(void)\n{\n    char str&#91;128];\n \n    printf(\"Enter a string: \");\n    scanf(\"%&#91;^o]s\", str);\n \n    printf(\"You entered: %s\\n\", str);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> &#91;root@centos-6 C]# .\/scan-set \n  Enter a string: http:\/\/Skill Vertex\n  You entered: http:\/\/Skill f\n  &#91;root@centos-6 C]# <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementation-of-gets-function-using-scanset\"> Implementation of gets() function using scanset<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can create a simple <code>gets()<\/code> function using scanf with a scanset to read a line from stdin. Here&#8217;s a basic implementation in C:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* implementation of gets() function using scanset *\/\n#include &lt;stdio.h&gt;\n \nint main(void)\n{\n    char str&#91;128];\n \n    printf(\"Enter a string with spaces: \");\n    scanf(\"%&#91;^\\n]s\", str);\n \n    printf(\"You entered: %s\\n\", str);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@centos-6 C]# .\/gets \n  Enter a string with spaces: Skill Vertex\n  You entered: Skill Vertex\n  &#91;root@centos-6 C]# <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-scansets-in-c\">FAQ- Scansets In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1695979810375\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the Scanset function in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. You can make a scanset by putting characters inside square brackets in C. It&#8217;s important to remember that scansets are case-sensitive, so uppercase and lowercase letters are treated differently. You can also use a comma to separate the characters you want to include in the scanset. Here&#8217;s an example:<br \/>For instance, <code>scanf(\"%s[A-Z,_,a,b,c]s\", str);<\/code> will scan and store characters that match the set [A-Z,_,a,b,c] in the variable <code>str<\/code><\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695979818944\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the syntax of scanf char in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The general syntax of scanf &#8211; int scanf(const char *format, Object *arg(s))<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695979827829\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. How to scan a string in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C, the <code>scanf<\/code> function with the &#8220;%s&#8221; format specifier stops reading a string at the first space. To accept a string with spaces in user input, you can use one of the following four common methods<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Scansets In C Scanset specifiers are used to specify a set of characters that Scanf should match and store in a variable. In C&#8217;s scanf family of functions, scanset specifiers are denoted by %[]. Inside a scanset, you can specify either single characters or a range of characters that you want to match and store. &#8230; <a title=\"Scansets In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/scansets-in-c\/\" aria-label=\"More on Scansets In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5333,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[396],"class_list":["post-2228","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-scansets-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2228"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2228\/revisions"}],"predecessor-version":[{"id":10639,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2228\/revisions\/10639"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5333"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}