{"id":11955,"date":"2022-09-29T03:11:08","date_gmt":"2022-09-28T21:41:08","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/"},"modified":"2022-09-29T03:11:08","modified_gmt":"2022-09-28T21:41:08","slug":"solved-split-string-by-one-of-few-delimiters-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/","title":{"rendered":"[Solved] Split string by one of few delimiters? [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-38675688\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"38675688\" data-parentid=\"38675046\" data-score=\"1\" data-position-on-page=\"2\" data-highest-scored=\"0\" data-question-has-accepted-highest-score=\"0\" itemprop=\"suggestedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Here is a generalized procedure:<\/p>\n<ol>\n<li>For a given set <code>delimiters<\/code>, use <code>strstr<\/code> to check each if it appears in the input string. As a bonus, my code below allows &#8216;double&#8217; entries such as <code>&lt;<\/code> and <code>&lt;&gt;<\/code>; it checks all and use the longest possible.<\/li>\n<li>After determining the best delimiter to use, you have a <em>pointer<\/em> to its start. Then you can<\/li>\n<li>.. copy everything at its left into a <code>left<\/code> variable;<\/li>\n<li>.. copy the delimiter itself into a <code>delim<\/code> variable (for consistency); and<\/li>\n<li>.. copy everything to the right of the delimiter into a <code>right<\/code> variable.<\/li>\n<\/ol>\n<p>Point 4 is &#8216;for consistency&#8217; with the other two variables. You could also create an enumeration (<code>LESS<\/code>, <code>EQUALS<\/code>, <code>MORE<\/code>, <code>NOT_EQUAL<\/code> (in my example)) and return that instead, because the set of possibilities is limited to these.<\/p>\n<p>In code:<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;ctype.h&gt;\n\nconst char *delimiters[] = {\n    \"&lt;\", \"&gt;\", \"==\", \"&lt;&gt;\", NULL\n};\n\nint split_string (const char *input, char **dest_left, char **dest_delim, char **dest_right)\n{\n    int iterator;\n    int best_fit_delim;\n    char *ptr;\n\n    \/* (optionally) clean whitespace at start *\/\n    while (isspace(*input))\n        input++;\n\n    \/* look for the longest delimiter we can find *\/\n    best_fit_delim = -1;\n    iterator = 0;\n    while (delimiters[iterator])\n    {\n        ptr = strstr (input, delimiters[iterator]);\n        if (ptr)\n        {\n            if (best_fit_delim == -1 || strlen(delimiters[iterator]) &gt; strlen(delimiters[best_fit_delim]))\n                best_fit_delim = iterator;\n        }\n        iterator++;\n    }\n\n    \/* did we find anything? *\/\n    if (best_fit_delim == -1)\n        return 0;\n\n    \/* reset ptr to this found one *\/\n    ptr = strstr (input, delimiters[best_fit_delim]);\n\n    \/* copy left hand side *\/\n    iterator = ptr - input;\n    \/* clean whitespace at end *\/\n    while (iterator &gt; 0 &amp;&amp; isspace(input[iterator-1]))\n        iterator--;\n    *dest_left = malloc (iterator + 1);\n    memcpy (*dest_left, input, iterator);\n    (*dest_left)[iterator] = 0;\n\n    \/* the delimiter itself *\/\n    *dest_delim = malloc(strlen(delimiters[best_fit_delim])+1);\n    strcpy (*dest_delim, delimiters[best_fit_delim]);\n\n    \/* update the pointer to point to *end* of delimiter *\/\n    ptr += strlen(delimiters[best_fit_delim]);\n    \/* skip whitespace at start *\/\n    while (isspace(*ptr))\n        ptr++;\n\n    \/* copy right hand side *\/\n    *dest_right = malloc (strlen(ptr) + 1);\n    strcpy (*dest_right, ptr);\n\n    return 1;\n}\n\nint main (void)\n{\n    char *source_str = \"A &lt;&gt; B\";\n    char *left, *delim, *right;\n\n    if (!split_string (source_str, &amp;left, &amp;delim, &amp;right))\n    {\n        printf (\"invalid input\\n\");\n    } else\n    {\n        printf (\"left: \\\"%s\\\"\\n\", left);\n        printf (\"delim: \\\"%s\\\"\\n\", delim);\n        printf (\"right: \\\"%s\\\"\\n\", right);\n\n        free (left);\n        free (delim);\n        free (right);\n    }\n    return 0;\n}\n<\/code><\/pre>\n<p>resulting, for <code>A &lt;&gt; B<\/code>, in<\/p>\n<pre><code>left: \"A\"\ndelim: \"&lt;&gt;\"\nright: \"B\"\n<\/code><\/pre>\n<p>The code can be a bit smaller if you only need to check your list of <code>&lt;<\/code>, <code>==<\/code>, and <code>&gt;<\/code>; then you can use <code>strchr<\/code>, for single characters (and if <code>=<\/code> is found, check the next character). You can also forget the <code>best_fit<\/code> length check, as there can be only one that fits.<\/p>\n<p>The code removes whitespace only around the comparison operator. For consistency, you may want to remove all whitespace at the start and end of the input; then, invalid input can be detected by the return <code>left<\/code> or <code>right<\/code> variables having a length of <code>0<\/code> \u2013 i.e., they only contain the <code>0<\/code> string terminator. You still need to <code>free<\/code> those zero-length strings.<\/p>\n<p>For fun, you can add <code>\"GT\",\"LT\",\"GE\",\"LE\"<\/code> to the delimiters and see how it does on strings such as <code>A GT B<\/code>, <code>ALLEQUAL<\/code>, and <code>FAULTY&lt;MATCH<\/code>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Split string by one of few delimiters? [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Here is a generalized procedure: For a given set delimiters, use strstr to check each if it appears in the input string. As a bonus, my code below allows &#8216;double&#8217; entries such as &lt; and &lt;&gt;; it checks all and use the longest possible. After determining the best delimiter to use, you have a &#8230; <a title=\"[Solved] Split string by one of few delimiters? [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/\" aria-label=\"More on [Solved] Split string by one of few delimiters? [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-11955","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Split string by one of few delimiters? [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Split string by one of few delimiters? [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Here is a generalized procedure: For a given set delimiters, use strstr to check each if it appears in the input string. As a bonus, my code below allows &#8216;double&#8217; entries such as &lt; and &lt;&gt;; it checks all and use the longest possible. After determining the best delimiter to use, you have a ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-28T21:41:08+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Split string by one of few delimiters? [closed]\",\"datePublished\":\"2022-09-28T21:41:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/\"},\"wordCount\":263,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/\",\"name\":\"[Solved] Split string by one of few delimiters? [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-28T21:41:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Split string by one of few delimiters? [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Split string by one of few delimiters? [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Split string by one of few delimiters? [closed] - JassWeb","og_description":"[ad_1] Here is a generalized procedure: For a given set delimiters, use strstr to check each if it appears in the input string. As a bonus, my code below allows &#8216;double&#8217; entries such as &lt; and &lt;&gt;; it checks all and use the longest possible. After determining the best delimiter to use, you have a ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/","og_site_name":"JassWeb","article_published_time":"2022-09-28T21:41:08+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Split string by one of few delimiters? [closed]","datePublished":"2022-09-28T21:41:08+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/"},"wordCount":263,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/","name":"[Solved] Split string by one of few delimiters? [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-28T21:41:08+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-split-string-by-one-of-few-delimiters-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Split string by one of few delimiters? [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/11955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=11955"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/11955\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=11955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=11955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=11955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}