{"id":7385,"date":"2026-02-08T00:01:39","date_gmt":"2026-02-08T08:01:39","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7385"},"modified":"2026-01-31T13:06:41","modified_gmt":"2026-01-31T21:06:41","slug":"pulling-numbers-from-a-string-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7385","title":{"rendered":"Pulling Numbers from a String &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7365\">C programming exercise<\/a> is to extract numbers from a string. Specifically, your task is to code the <em>extract()<\/em> function that returns the address of a digit found in a string. This function must also be capable of repeat calls to continue locating digits within the same string.<br \/>\n<!--more--><br \/>\nHere is my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_02-Exercise.c\" rel=\"noopener\" target=\"_blank\">2026_02-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;ctype.h&gt;\r\n\r\n<span class=\"comments\">\/* return the first location of a number\r\n   in string s *\/<\/span>\r\nchar *extract(char *s)\r\n{\r\n    static char *sp = NULL;\r\n\r\n    <span class=\"comments\">\/* check for recall *\/<\/span>\r\n    if( s != NULL )\r\n        sp = s;\r\n    else\r\n    {\r\n        <span class=\"comments\">\/* guard against a NULL string\r\n           passed the first time *\/<\/span>\r\n        if (sp==NULL)\r\n            return(NULL);\r\n        <span class=\"comments\">\/* find the next non-digit *\/<\/span>\r\n        while( isdigit(*sp) )\r\n            sp++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* find the next digit *\/<\/span>\r\n    while( *sp != '\\0' )\r\n    {\r\n        if( isdigit(*sp) )\r\n        {\r\n            return(sp);\r\n        }\r\n        sp++;\r\n    }\r\n\r\n    return(NULL);\r\n}\r\n\r\nint main()\r\n{\r\n    char sample[] = \"abc10=13!260;1m\";\r\n    char *r;\r\n    int v;\r\n\r\n    r = extract(sample);\r\n    if( r != NULL )\r\n    {\r\n        v = atoi(r);\r\n        printf(\"%d\\n\",v);\r\n        while( (r=extract(NULL)) )\r\n        {\r\n            v = atoi(r);\r\n            printf(\"%d\\n\",v);\r\n        }\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>main()<\/em> function contains a sample string to examine: <code>\"abc10=13!260;1m\"<\/code><\/p>\n<p>After the initial call to the <em>extract()<\/em> function, an <em>if<\/em> test checks for a NULL result. If true, the string doesn&#8217;t contain any numbers; the program is over. Otherwise, the value returned is stored in <em>char<\/em> pointer <code>r<\/code>. The <em>atoi()<\/em> function extracts this value and <em>printf()<\/em> sends it to standard output. Then a <em>while<\/em> loop repeatedly calls the <em>extract()<\/em> function to look for and output additional values until the string is exhausted:<\/p>\n<p><code>while( (r=extract(NULL)) )<\/code><\/p>\n<p>When called with NULL, the <em>extract()<\/em> function continues searching the string. The loop stops once the function returns NULL, which is why the expression is enclosed in parentheses.<\/p>\n<p>The <em>extract()<\/em> function accepts the string to examine as argument <code>*s<\/code> and returns a pointer to the first digit found:<\/p>\n<p><code>char *extract(char *s)<\/code><\/p>\n<p>Within the function, <em>static char<\/em> pointer <code>sp<\/code> is initialized with the value NULL. As a <em>static<\/em> variable, the pointer&#8217;s value is maintained within the function, but it&#8217;s only initialized to NULL the first time the function is called.<\/p>\n<p>The function has two parts. The first deals with a recall to the function. The second finds a digit in the string.<\/p>\n<p>To check for a recall, an <em>if-else <\/em>structure is used. The <em>if<\/em> test checks whether the value of variable <code>s<\/code> (the passed pointer) is NULL:<\/p>\n<p><code>if( s != NULL )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;sp = s;<\/code><\/p>\n<p>When <code>s<\/code> is not NULL, the assumption is that the function is being called for the first time. In this instance, pointer <code>sp<\/code> is initialized to the address of <code>s<\/code>. Otherwise, the assumption is that the function is being called again and that pointer <em>sp<\/em> is already initialized.<\/p>\n<p>The <em>else<\/em> part of the structure assumes that pointer <code>sp<\/code> is initialized and already references a digit in the string. (The function is being called again after initial success.) This code block&#8217;s job is to skip past any further digits in the string before looking for another digit. The <em>while<\/em> loop spins until a non-digit is found:<\/p>\n<p><code>while( isdigit(*sp) )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;sp++;<\/code><\/p>\n<p>The second part of the <em>extract()<\/em> function locates the first or next digit in the string. A <em>while<\/em> loop spins as long as the value of <code>*sp<\/code> isn&#8217;t the terminating null character:<\/p>\n<p><code>while( *sp != '\\0' )<\/code><\/p>\n<p>In the loop, an <em>if<\/em> test checks to see whether <code>*sp<\/code> references a digit. If true, the address in <code>sp<\/code> is returned. Otherwise, the pointer&#8217;s address is incremented to continue looking for a digit.<\/p>\n<p>When all options are exhausted, the function returns a NULL pointer.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre>10\r\n13\r\n260\r\n1<\/pre>\n<p>The <em>extract()<\/em> function works to yank out integers from any string no matter where the digits lie within the string. This type of function is useful to extract unformatted data, such as the string generates when using ANSI commands to monitor mouse movement in a terminal window.<\/p>\n<p>I hope that your solution met with success.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s C programming exercise is to extract numbers from a string. Specifically, your task is to code the extract() function that returns the address of a digit found in a string. This function must also be capable of repeat &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7385\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-7385","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7385"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7385\/revisions"}],"predecessor-version":[{"id":7403,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7385\/revisions\/7403"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}