{"id":2004,"date":"2015-06-09T07:33:12","date_gmt":"2015-06-09T07:33:12","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2004"},"modified":"2026-06-17T09:15:26","modified_gmt":"2026-06-17T08:15:26","slug":"strings-general","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/strings\/strings-general","title":{"rendered":".Char based Strings General"},"content":{"rendered":"<h4>\nHeader Files<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n#include &lt;string.h&gt;\r\n<\/code><\/pre>\n<h4>\nCreating Strings<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tstatic const char *my_define = &quot;\/etc\/sysconfig\/network&quot;;\r\n\r\n\tconst char *mytext = &quot;Hello World&quot;;\r\n<\/code><\/pre>\n<h4>\nString Length<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tint mytext_length = strlen(mytext);\r\n<\/code><\/pre>\n<h4>\nDoes String Contain<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tif (strncmp(*++argv, &quot;O_RDONLY&quot;, 8) == 0)\r\n\t\topen_mode |= O_RDONLY;\r\n<\/code><\/pre>\n<h4>\nCopy String<br \/>\n<\/h4>\n<h5>\nThe preferred way<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tstrncpy (string_destination, string_source, max_length);\t\t\/\/Note no null will be included if the string is &gt;= max_length\r\n<\/code><\/pre>\n<h5>\nThe more dangerous way<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tstrcpy (string_destination, string_source);\r\n<\/code><\/pre>\n<h4>\nReturn A Char Array From A Function<br \/>\n<\/h4>\n<p>\nYou need to dynamically allocate your char array.\n<\/p>\n<p>\nFor C++:\n<\/p>\n<pre>\r\n<code>\r\nchar *recvmsg(){\r\n   char* buffer = new char[1024];\r\n   return buffer;\r\n}\r\n<\/code><\/pre>\n<p>\nFor C:\n<\/p>\n<pre>\r\n<code>\r\nchar *recvmsg(){\r\n   char* buffer = malloc(1024);\r\n   return buffer;\r\n}\r\n<\/code><\/pre>\n<p>\nWithout this dynamic allocation the variable will reside on the function&#39;s stack and will therefore be destroyed on exit. This is why you will typically get a warning if you do this.&nbsp;Allocating it on the heap prevents this, BUT&nbsp;you will have to be careful and free the memory once done with it via delete[].\n<\/p>\n<h4>\nReplace String With String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\/\/************************************************\r\n\/\/************************************************\r\n\/\/********** REPLACE STRING WITH STRING **********\r\n\/\/************************************************\r\n\/\/************************************************\r\n\/\/Look for first occurrence of a string and replace it with a new string\r\n\/\/The new outputted string is stored in original_string\r\nint string_replace_string_with_string (char *original_string, char *find_string, char *replace_with_string)\r\n{\r\n\tchar buffer[MAX_STRING_LENGTH];\r\n\tchar *p;\r\n\r\n\tp = strstr(original_string, find_string);\t\t\t\t\t\t\/\/Find first occurrence of the string\r\n\tif (p)\r\n\t{\r\n\t\tstrncpy(buffer, original_string, p - original_string);\t\t\/\/Copy characters from original_string start to find_string st$\r\n\t\tbuffer[p - original_string] = &#39;&#39;;\r\n\t\tsnprintf(buffer + (p - original_string),\r\n\t\t\t\t(MAX_STRING_LENGTH - (int)(buffer + (p - original_string))),\r\n\t\t\t\t&quot;%s%s&quot;,\r\n\t\t\t\treplace_with_string,\r\n\t\t\t\tp + strlen(find_string));\r\n\r\n\t\tstrncpy(original_string, buffer, MAX_STRING_LENGTH);\t\t\/\/Copy back into the original string\r\n\t\treturn(1);\r\n\t}\r\n\treturn(0);\r\n}\r\n\/* Using the function:\r\n#define\tMAX_STRING_LENGTH \t1024\r\n\r\nchar original_string[MAX_STRING_LENGTH] = {&quot;There are many variations of passages of Lorem Ipsum available, but are the majority have suffered alteration in some form&quot;};\r\nchar find_string[] = {&quot;are&quot;};\r\nchar replace_with_string[] = {&quot;z&quot;};\r\n\r\nstring_replace_string_with_string (original_string, find_string, replace_with_string, MAX_STRING_LENGTH);\r\nprintf(&quot;nnMODIFIED STRING:n%sn&quot;, original_string);\r\n*\/\r\n<\/code><\/pre>\n<h4>\nFind String Between Strings<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\/\/*************************************************\r\n\/\/*************************************************\r\n\/\/********** FIND STRING BETWEEN STRINGS **********\r\n\/\/*************************************************\r\n\/\/*************************************************\r\n\/\/Looks for first occurrence of a string1 and string2 and outputs the string between them\r\nint string_find_string_between_strings (char *source_string, char *find_string1, char *find_string2, char *result_string)\r\n{\r\n\tchar *p_1;\r\n\tchar *p_2;\r\n\r\n\t\/\/Default to a null output string\r\n\t*result_string = &#39;&#39;;\r\n\r\n\tp_1 = strstr(source_string, find_string1);\r\n\tp_2 = strstr(source_string, find_string2);\r\n\r\n\tif (p_1 &amp;&amp; p_2)\r\n\t{\r\n\t\t\/\/Exclude the search strings from the result\r\n\t\tp_1 += strlen(find_string1);\r\n\r\n\t\t\/\/Ensure they are the right way round\r\n\t\tif (p_2 &lt; p_1)\r\n\t\t\treturn(0);\r\n\r\n\t\tint length = (p_2 - p_1);\r\n\t\tif (length &gt; (MAX_STRING_LENGTH - 1))\r\n\t\t\tlength = (MAX_STRING_LENGTH - 1);\r\n\r\n\t\t\/\/Ensure the final byte is null\r\n\t\tresult_string[length]  = &#39;&#39;;\r\n\r\n\r\n\t\tstrncpy(result_string, p_1, length);\t\t\/\/Copy characters from original_string start to find_string st$\r\n\r\n\t\treturn(1);\r\n\t}\r\n\treturn(0);\r\n}\r\n\/* Using the function:\r\n\t#define\tMAX_STRING_LENGTH \t1024\r\n\r\n\tchar original_string[] = {&quot;There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form&quot;};\r\n\tchar find_string1[] = {&quot;are &quot;};\r\n\tchar find_string2[] = {&quot; but&quot;};\r\n\tchar result_string[MAX_STRING_LENGTH];\r\n\r\n\tstring_find_string_between_strings(original_string, find_string1, find_string2, result_string, MAX_STRING_LENGTH);\r\n\tprintf(&quot;nFOUND STRING:n[%s]n&quot;, result_string);\r\n *\/\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<h4>\nGet String Up To String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\/\/*********************************************\r\n\/\/*********************************************\r\n\/\/********** GET STRING UP TO STRING **********\r\n\/\/*********************************************\r\n\/\/*********************************************\r\n\/\/Looks for first occurrence of a string and outputs the string up to that string\r\nint string_get_string_up_to_string (char *source_string, char *find_string, char *result_string)\r\n{\r\n\tchar *p;\r\n\r\n\t\/\/Default to a null output string\r\n\t*result_string = &#39;&#39;;\r\n\r\n\tp = strstr(source_string, find_string);\r\n\tif (p)\r\n\t{\r\n\t\tint length = (p - source_string);\r\n\t\tif (length &gt; (MAX_STRING_LENGTH - 1))\r\n\t\t\tlength = (MAX_STRING_LENGTH - 1);\r\n\r\n\t\t\/\/Ensure the final byte is null (won&#39;t be added by strncpy if resutlt &gt; MAX_STRING_LENGTH)\r\n\t\tresult_string[length]  = &#39;&#39;;\r\n\r\n\r\n\t\tstrncpy(result_string, source_string, length);\t\t\/\/Copy characters from original_string start to find_string st$\r\n\r\n\t\treturn(1);\r\n\t}\r\n\treturn(0);\r\n}\r\n\/* Using the function:\r\n#define\tMAX_STRING_LENGTH \t1024\r\n\r\nchar source_string[] = {&quot;There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form&quot;};\r\nchar find_string[] = {&quot; but &quot;};\r\nchar result_string[MAX_STRING_LENGTH];\r\n\r\n\tstring_get_string_up_to_string (source_string, find_string, result_string);\r\n\tprintf(&quot;nBefore string:n[%s]n&quot;, result_string);\r\n*\/\r\n<\/code><\/pre>\n<h4>\nGet String After String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\/\/*********************************************\r\n\/\/*********************************************\r\n\/\/********** GET STRING AFTER STRING **********\r\n\/\/*********************************************\r\n\/\/*********************************************\r\n\/\/Looks for first occurrence of a string and outputs the string after that string\r\nint string_get_string_after_string (char *source_string, char *find_string, char *result_string)\r\n{\r\n\tchar *p;\r\n\r\n\t\/\/Default to a null output string\r\n\t*result_string = &#39;&#39;;\r\n\r\n\tp = strstr(source_string, find_string);\r\n\tif (p)\r\n\t{\r\n\t\tp += strlen(find_string);\r\n\r\n\t\t\/\/Ensure the final byte is null (won&#39;t be added by strncpy if resutlt &gt; MAX_STRING_LENGTH)\r\n\t\tresult_string[(MAX_STRING_LENGTH - 1)]  = &#39;&#39;;\r\n\r\n\t\tstrncpy(result_string, p, (MAX_STRING_LENGTH - 1));\t\t\/\/Copy characters from original_string start to find_string st$\r\n\r\n\t\treturn(1);\r\n\t}\r\n\treturn(0);\r\n}\r\n\/* Using the function:\r\n#define\tMAX_STRING_LENGTH \t1024\r\n\r\nchar source_string[] = {&quot;There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form&quot;};\r\nchar find_string[] = {&quot; but &quot;};\r\nchar result_string[MAX_STRING_LENGTH];\r\n\r\n\tstring_get_string_after_string (source_string, find_string, result_string);\r\n\tprintf(&quot;nAfter string:n[%s]n&quot;, result_string);\r\n*\/\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Header Files #include &lt;string.h&gt; Creating Strings static const char *my_define = &quot;\/etc\/sysconfig\/network&quot;; const char *mytext = &quot;Hello World&quot;; String Length int mytext_length = strlen(mytext); Does String Contain if (strncmp(*++argv, &quot;O_RDONLY&quot;, 8) == 0) open_mode |= O_RDONLY; Copy String The preferred way strncpy (string_destination, string_source, max_length); \/\/Note no null will be included if the string is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-2004","post","type-post","status-publish","format-standard","hentry","category-strings"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2004"}],"version-history":[{"count":7,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2004\/revisions"}],"predecessor-version":[{"id":2192,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2004\/revisions\/2192"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}