{"id":8136,"date":"2022-04-14T14:39:22","date_gmt":"2022-04-14T14:39:22","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=8136"},"modified":"2022-06-20T09:08:51","modified_gmt":"2022-06-20T09:08:51","slug":"how-to-write-string-programs-in-c","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/","title":{"rendered":"How to write string programs in C"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647929754914-B_275.png\" alt=\"\" \/><\/p>\n<p>A String in C is basically a sequence of characters\/symbols which is terminated with a null character \\0. <\/p>\n<\/p>\n<p>E.g., char str = \u201cthis is a string\u201d<\/p>\n<p><strong>Let us see how to declare a string in C.<\/strong><br \/>\nDeclare string variable<\/p>\n<pre><code>char str[16] = &quot;This is a string&quot; \nchar str[] = &quot;This is a string&quot; <\/code><\/pre>\n<h3>Taking Inputs &#8211;<\/h3>\n<ul>\n<li><strong>scanf()<\/strong> : this function is used to take string inputs in C.<br \/>\n<em>char str[30] ; scanf(%s,\u201dThis is a string\u201d)<\/em><br \/>\nThis function is useful only when our string does not contain any spaces. In the above statement only \u201cThis\u201d will be taken from the user.<\/li>\n<li><strong>fgets()<\/strong> : we can use this function even if we have spaces. Mostly this function is followed by a puts() function to print the entered string.<\/li>\n<\/ul>\n<h4>Code Implementation &#8211;<\/h4>\n<pre><code>#include &lt;stdio.h&gt;\nint main(void) {\n    \/\/ char str[30];\n    \/\/ scanf(&quot;%s&quot;,str);\n    \/\/ printf(&quot;%s&quot;,str);\n    char str1[30];\n    fgets(str1,sizeof(str1), stdin);\n    puts(str1); \/\/prints the string\n    return 0;\n}\nOutput: This is a string<\/code><\/pre>\n<h3>Accessing each elements of a string in C<\/h3>\n<p>Well, each of the characters are stored in a position contiguously. So, if we find out the length of the string then we can simply use a for loop to iterate through the elements of the string. We use strlen() function of string header file in C to get the length of our string.<\/p>\n<h4>Code Implementation \u2013<\/h4>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\nint main(void) {\n    char str[30];\n    fgets(str,sizeof(str), stdin);\n    \/\/acessing each elements of the string\n    for(int i = 0;i&lt;strlen(str);i++)\n        printf(&quot;%c&quot;,str[i]);\n    return 0;\n}<\/code><\/pre>\n<p>Now when we know to write basic program lets see an example \u2013<\/p>\n<p>Suppose we are given a string and we want to find the number of spaces in the string.<br \/>\nThe idea is to iterate over the string elements and whenever we get a \u2018(space) \u2018character we increase our count.<\/p>\n<h4>Code Implementation \u2013<\/h4>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\nint main(void) {\n    char str[30];\n    fgets(str,sizeof(str), stdin);\n    int count = 0;\n    \/\/acessing each elements of the string\n    for(int i = 0;i&lt;strlen(str);i++)\n    {\n        if(str[i] == &#039; &#039;)\n            count++;\n    }\n    printf(&quot;%d&quot;, count);\n    return 0;\n} <\/code><\/pre>\n<p><strong>Time Complexity<\/strong>: O(length(str))<\/p>\n<p>Another interesting example can be concatenating two strings.<br \/>\nWe can do this manually by adding the characters of string 2 to the end of string 1.<br \/>\nOr we can directly use the strcat() function to do this. This function takes two arguments string 1 and string 2 and concatenates them and stores the result in string 1.<\/p>\n<h4>Code Implementation \u2013<\/h4>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\nint main() {\n  char s1[100] = &quot;This is string1 &quot;, s2[] = &quot;This is string 2&quot;;\n  int length, j;\n  length = strlen(s1);\n  for (j = 0; s2[j] != &#039;\\0&#039;; ++j, ++length) {\n    s1[length] = s2[j];\n  }\n\/\/or simply\n\/\/strcat(s1,s2);\n  s1[length] = &#039;\\0&#039;;\n  puts(s1);\n  return 0;\n}<\/code><\/pre>\n<p><strong>Time Complexity<\/strong>: O(length(str)) <\/p>\n<p>This article tried to discuss <strong>how to implement string in C langauage<\/strong>. Hope this blog helps you understand the implementation. To practice more problems you can check out <a href=\"#\"><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A String in C is basically a sequence of characters\/symbols which is terminated with a null character \\0. E.g., char str = \u201cthis is a string\u201d Let us see how to declare a string in C. Declare string variable char str[16] = &quot;This is a string&quot; char str[] = &quot;This is a string&quot; Taking Inputs [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-8136","post","type-post","status-publish","format-standard","hentry","category-c-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to write string programs in C | C Programming | Prepbytes<\/title>\n<meta name=\"description\" content=\"A String in C is basically a sequence of characters\/symbols which is terminated with a null character . E.g., char str = \u201cthis is a string\u201d\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to write string programs in C | C Programming | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"A String in C is basically a sequence of characters\/symbols which is terminated with a null character . E.g., char str = \u201cthis is a string\u201d\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-04-14T14:39:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-06-20T09:08:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647929754914-B_275.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\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:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"How to write string programs in C\",\"datePublished\":\"2022-04-14T14:39:22+00:00\",\"dateModified\":\"2022-06-20T09:08:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/\"},\"wordCount\":329,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647929754914-B_275.png\",\"articleSection\":[\"C Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/\",\"name\":\"How to write string programs in C | C Programming | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647929754914-B_275.png\",\"datePublished\":\"2022-04-14T14:39:22+00:00\",\"dateModified\":\"2022-06-20T09:08:51+00:00\",\"description\":\"A String in C is basically a sequence of characters\/symbols which is terminated with a null character \\\\0. E.g., char str = \u201cthis is a string\u201d\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647929754914-B_275.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647929754914-B_275.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C Programming\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/c-programming\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to write string programs in C\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to write string programs in C | C Programming | Prepbytes","description":"A String in C is basically a sequence of characters\/symbols which is terminated with a null character . E.g., char str = \u201cthis is a string\u201d","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:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/","og_locale":"en_US","og_type":"article","og_title":"How to write string programs in C | C Programming | Prepbytes","og_description":"A String in C is basically a sequence of characters\/symbols which is terminated with a null character . E.g., char str = \u201cthis is a string\u201d","og_url":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-04-14T14:39:22+00:00","article_modified_time":"2022-06-20T09:08:51+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647929754914-B_275.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"How to write string programs in C","datePublished":"2022-04-14T14:39:22+00:00","dateModified":"2022-06-20T09:08:51+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/"},"wordCount":329,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647929754914-B_275.png","articleSection":["C Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/","url":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/","name":"How to write string programs in C | C Programming | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647929754914-B_275.png","datePublished":"2022-04-14T14:39:22+00:00","dateModified":"2022-06-20T09:08:51+00:00","description":"A String in C is basically a sequence of characters\/symbols which is terminated with a null character \\0. E.g., char str = \u201cthis is a string\u201d","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647929754914-B_275.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647929754914-B_275.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/how-to-write-string-programs-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"C Programming","item":"https:\/\/prepbytes.com\/blog\/category\/c-programming\/"},{"@type":"ListItem","position":3,"name":"How to write string programs in C"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/8136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=8136"}],"version-history":[{"count":2,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/8136\/revisions"}],"predecessor-version":[{"id":8559,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/8136\/revisions\/8559"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=8136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=8136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=8136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}