{"id":26540,"date":"2017-05-31T18:56:11","date_gmt":"2017-05-31T13:26:11","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26540"},"modified":"2017-05-31T18:56:11","modified_gmt":"2017-05-31T13:26:11","slug":"c-program-to-convert-string-to-integer-without-using-atoi-function","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-program-to-convert-string-to-integer-without-using-atoi-function\/","title":{"rendered":"C program to convert string to integer without using atoi function"},"content":{"rendered":"<p>C program to <a href=\"https:\/\/www.wikitechy.com\/technology\/c-program-for-palindrome\/\">convert string to integer<\/a> without using atoi function: It is frequently required to convert a string to an integer in applications. String should consists of digits only and an optional \u2018-\u2018 (minus) sign at beginning for integers. For string containing other characters we can stop conversion as soon as a non digit character is encountered but in our program we will handle ideal case when only valid characters are present in string. Library function atoi can be used to convert string to an integer but we will create our own function.<\/p>\n[ad type=\u201dbanner\u201d]\n<h2 id=\"c-programming-code\">C programming code<\/h2>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20C%20program%20to%20convert%20string%20to%20integer%20without%20using%20atoi%20function%0A%23include%20%3Cstdio.h%3E%0A%20%0Aint%20toString(char%20%5B%5D)%3B%0A%20%0Aint%20main()%0A%7B%0A%20%20char%20a%5B100%5D%3B%0A%20%20int%20n%3B%0A%20%0A%20%20printf(%22Input%20a%20valid%20string%20to%20convert%20to%20integer%5Cn%22)%3B%0A%20%20scanf(%22%25s%22%2C%20a)%3B%0A%20%0A%20%20n%20%3D%20toString(a)%3B%0A%20%0A%20%20printf(%22String%20%20%3D%20%25s%5CnInteger%20%3D%20%25d%5Cn%22%2C%20a%2C%20n)%3B%0A%20%0A%20%20return%200%3B%0A%7D%0A%20%0Aint%20toString(char%20a%5B%5D)%20%7B%0A%20%20int%20c%2C%20sign%2C%20offset%2C%20n%3B%0A%20%0A%20%20if%20(a%5B0%5D%20%3D%3D%20\u2032-\u2018)%20%7B%20%20%2F%2F%20Handle%20negative%20integers%0A%20%20%20%20sign%20%3D%20-1%3B%0A%20%20%7D%0A%20%0A%20%20if%20(sign%20%3D%3D%20-1)%20%7B%20%20%2F%2F%20Set%20starting%20position%20to%20convert%0A%20%20%20%20offset%20%3D%201%3B%0A%20%20%7D%0A%20%20else%20%7B%0A%20%20%20%20offset%20%3D%200%3B%0A%20%20%7D%0A%20%0A%20%20n%20%3D%200%3B%0A%20%0A%20%20for%20(c%20%3D%20offset%3B%20a%5Bc%5D%20!%3D%20\u2019%5C0\u2019%3B%20c%2B%2B)%20%7B%0A%20%20%20%20n%20%3D%20n%20*%2010%20%2B%20a%5Bc%5D%20-%20\u20190\u2019%3B%0A%20%20%7D%0A%20%0A%20%20if%20(sign%20%3D%3D%20-1)%20%7B%0A%20%20%20%20n%20%3D%20-n%3B%0A%20%20%7D%0A%20%0A%20%20return%20n%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Similarly you can convert string to long.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Output of program:<\/strong><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-large wp-image-26541\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/string-to-integer-c-program-1024x155.png\" alt=\"C program to convert string to integer without using atoi function\" width=\"625\" height=\"95\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/string-to-integer-c-program-1024x155.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/string-to-integer-c-program-300x46.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/string-to-integer-c-program-768x117.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/string-to-integer-c-program-990x150.png 990w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/string-to-integer-c-program.png 1212w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C program to convert string to integer without using atoi function-C Programming-It is frequently required to convert a string to an integer in application.<\/p>\n","protected":false},"author":2,"featured_media":26562,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866],"tags":[79128,79134,79137,79130,79122,79139,79129,79126,79124,79133,79131,79132,79141,79123,79135,79136,79125,79138,79142,79140,79127],"class_list":["post-26540","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-atoi-function-in-c","tag-c-convert-string-to-integer","tag-c-program-to-convert-string-to-integer","tag-c-program-to-convert-string-to-integer-without-using-atoi-function","tag-c-program-to-convert-string-to-integer-without-using-library-functions","tag-c-string-to-integer","tag-c-strtol","tag-convert-string-to-float-in-c","tag-convert-string-to-int-in-c-without-atoi","tag-convert-string-to-integer","tag-convert-string-to-integer-c","tag-convert-string-to-integer-in-c","tag-cstring-to-integer","tag-how-to-convert-int-to-string-in-c","tag-how-to-convert-string-to-integer","tag-how-to-convert-string-to-integer-in-c","tag-program-to-convert-string-to-integer-in-java","tag-string-to-integer","tag-string-to-integer-conversion-in-c","tag-string-to-integer-in-c","tag-strtonum-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26540","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=26540"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26540\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/26562"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}