{"id":27188,"date":"2017-06-01T19:28:39","date_gmt":"2017-06-01T13:58:39","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27188"},"modified":"2017-06-01T19:28:39","modified_gmt":"2017-06-01T13:58:39","slug":"c-program-remove-spaces-or-blanks-from-a-string","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-program-remove-spaces-or-blanks-from-a-string\/","title":{"rendered":"C program remove spaces or blanks from a string"},"content":{"rendered":"<p>C program remove spaces or blanks from a string, For example consider the string<\/p>\n<pre>\"c  programming\"<\/pre>\n<p>There are two spaces in this <a href=\"https:\/\/www.wikitechy.com\/technology\/substring-in-c-programming\/\">string<\/a>, so our program will print a string<\/p>\n<p>&#8220;c programming&#8221;. It will remove spaces when they occur more than one time consecutively in string anywhere.<\/p>\n[ad type=&#8221;banner&#8221;]\n<h2 id=\"c-programming-code\">C programming code<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt;<br\/> <br\/>int main()<br\/>{<br\/>   char text[1000], blank[1000];<br\/>   int c = 0, d = 0;<br\/> <br\/>   printf(&quot;Enter some text\\n&quot;);<br\/>   gets(text);<br\/> <br\/>   while (text[c] != &#039;\\0&#039;) {<br\/>      if (text[c] == &#039; &#039;) {<br\/>         int temp = c + 1;<br\/>         if (text[temp] != &#039;\\0&#039;) {<br\/>            while (text[temp] == &#039; &#039; &amp;&amp; text[temp] != &#039;\\0&#039;) {<br\/>               if (text[temp] == &#039; &#039;) {<br\/>                  c++;<br\/>               }  <br\/>               temp++;<br\/>            }<br\/>         }<br\/>      }<br\/>      blank[d] = text[c];<br\/>      c++;<br\/>      d++;<br\/>   }<br\/> <br\/>   blank[d] = &#039;\\0&#039;;<br\/> <br\/>   printf(&quot;Text after removing blanks\\n%s\\n&quot;, blank);<br\/> <br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>If you want you can copy blank into text string so that original string is modified.<\/p>\n<p><strong>Output of program:<\/strong><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-27193\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/remove-spaces-c.png\" alt=\"C program remove spaces or blanks from a string\" width=\"437\" height=\"129\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/remove-spaces-c.png 437w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/remove-spaces-c-300x89.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/remove-spaces-c-434x129.png 434w\" sizes=\"(max-width: 437px) 100vw, 437px\" \/><\/p>\n<h2 id=\"c-programming-code-using-pointers\">C programming code using pointers<\/h2>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt;<br\/>#include &lt;string.h&gt;<br\/>#include &lt;stdlib.h&gt;<br\/>#define SPACE &#039; &#039;<br\/> <br\/>char *process(char*);<br\/> <br\/>int main()<br\/>{<br\/>   char text[1000], *r;<br\/> <br\/>   printf(&quot;Enter a string\\n&quot;);<br\/>   gets(text);<br\/> <br\/>   r = process(text);<br\/> <br\/>   printf(&quot;\\&quot;%s\\&quot;\\n&quot;, r);<br\/> <br\/>   free(r);     <br\/> <br\/>   return 0;<br\/>}<br\/> <br\/>char *process(char *text) {<br\/>   int length, c, d;<br\/>   char *start;<br\/> <br\/>   c = d = 0;<br\/> <br\/>   length = strlen(text);<br\/> <br\/>   start = (char*)malloc(length+1);<br\/> <br\/>   if (start == NULL)<br\/>      exit(EXIT_FAILURE);<br\/> <br\/>   while (*(text+c) != &#039;\\0&#039;) {<br\/>      if (*(text+c) == &#039; &#039;) {<br\/>         int temp = c + 1;<br\/>         if (*(text+temp) != &#039;\\0&#039;) {<br\/>            while (*(text+temp) == &#039; &#039; &amp;&amp; *(text+temp) != &#039;\\0&#039;) {<br\/>               if (*(text+temp) == &#039; &#039;) {<br\/>                  c++;<br\/>               }  <br\/>               temp++;<br\/>            }<br\/>         }<br\/>      }<br\/>      *(start+d) = *(text+c);<br\/>      c++;<br\/>      d++;<br\/>   }<br\/>   *(start+d)= &#039;\\0&#039;;<br\/> <br\/>   return start;<br\/>}<\/code><\/pre> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>C program remove spaces or blanks from a string &#8211; C Programming &#8211; There are two spaces in this string, so our program will print a string.<\/p>\n","protected":false},"author":2,"featured_media":27213,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866],"tags":[81112,81114,81113,81108,81106,81110,81116,81111,81107,81115,81117,81105,81109,81104],"class_list":["post-27188","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-program-remove-spaces","tag-c-program-remove-spaces-blanks-from-a-string","tag-c-program-to-remove-spaces-in-a-string","tag-c-program-to-remove-white-spaces-from-file","tag-how-to-print-a-string-without-spaces-in-c","tag-program-to-remove-spaces-in-a-string-in-java","tag-python-remove-spaces-from-string","tag-remove-space-from-string-in-excel","tag-remove-space-from-string-java","tag-remove-spaces","tag-remove-spaces-from-text","tag-removing-spaces-from-a-string-c","tag-trim-function-in-c","tag-write-a-c-program-to-remove-the-unnecessary-spaces-in-a-given-sentence"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27188"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27188\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/27213"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}