Easy Way to Convert Int to String C

This is a C Program which converts an integer to string & vice-versa.

Problem Description

This C Program converts an integer to string and vice-versa.

Problem Solution

Take input from the user and perform string operations as shown in the program below.

Program/Source Code

Here is a source code of the C program to convert an integer to string & vice-versa. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

              /*  * C Program which Converts an Integer to String & vice-versa  */              #include <stdio.h>              #include <string.h>              #include <math.h>              void              tostring(              char              [              ]              ,              int              )              ;              int              toint(              char              [              ]              )              ;              int              main(              )              {              char              str[              10              ]              ;              int              num,              result;              printf              (              "Enter a number: "              )              ;              scanf              (              "%d"              ,              &num)              ;              tostring(str,              num)              ;              printf              (              "Number converted to string: %s\n"              ,              str)              ;              result              =              toint(str)              ;              printf              (              "Number converted back to integer: %d\n"              ,              result)              ;              return              0              ;              }              void              tostring(              char              str[              ]              ,              int              num)              {              int              i,              rem,              len              =              0              ,              n;              n              =              num;              while              (n              !=              0              )              {              len++;              n              /=              10              ;              }              for              (i              =              0              ;              i              <              len;              i++              )              {              rem              =              num              %              10              ;              num              =              num              /              10              ;              str[len              -              (i              +              1              )              ]              =              rem              +              '0'              ;              }              str[len]              =              '\0'              ;              }              int              toint(              char              str[              ]              )              {              int              len              =              strlen              (str)              ;              int              i,              num              =              0              ;              for              (i              =              0              ;              i              <              len;              i++              )              {              num              =              num              +              (              (str[len              -              (i              +              1              )              ]              -              '0'              )              *              pow              (              10              ,              i)              )              ;              }              return              num;              }            

Program Explanation

In this C program, we are reading the number using 'num' variable. The tostring() function is used to convert an integer to string & vice-versa.

Using tostring() function convert an integer to string. Assign the value of 'num' variable to 'n' variable. While loop is used to check the value of 'n' variable is not equal to 0. If the condition is true then execute the loop.
Compute the value of 'n' variable by 10 and assign the value to 'n' variable. For loop is used to convert an integer to string. Initialize the value of 'i' variable as 0 and check the value of 'i' variable is less than the value of 'len' variable. If the condition is true then execute the loop.

Compute the modulus of the value of 'num' variable by 10 and assign the value to 'rem' variable. Divide the value of 'num' variable by 10 and assign the value to 'num' variable. Print the statement as the string converted from an integer.

The toint() function is used to convert an integer to string & vice-versa. Assign the length of the string to 'len' variable. For loop is used to convert the string to an integer. Initialize the value of 'i' variable to 0 and check the value of 'i' variable is less than the value of 'len' variable.

If the condition is true then execute the loop using 'num' variable. Compute the power value of 10 to the power of the value of 'i' variable. Multiply the length of the string with the power value. Add the value with the 'num' variable and return the value of 'num' variable value. Print the statement as an integer to string and viceversa.

Runtime Test Cases

  $ gcc stringtoint.c -lm $ ./a.out Enter a number: 12345 Number converted to string: 12345 Number converted back to integer: 12345

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here's the list of Best Books in C Programming, Data-Structures and Algorithms

Manish Bhojasia - Founder & CTO at Sanfoundry

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

guerrerobirear.blogspot.com

Source: https://www.sanfoundry.com/c-program-integer-to-string-vice-versa/

0 Response to "Easy Way to Convert Int to String C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel