A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2FString%2FString_Convert</id>
		<title>C/String/String Convert - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2FString%2FString_Convert"/>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C/String/String_Convert&amp;action=history"/>
		<updated>2026-04-04T06:10:39Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://cppe.ru/index.php?title=C/String/String_Convert&amp;diff=146&amp;oldid=prev</id>
		<title> в 14:20, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C/String/String_Convert&amp;diff=146&amp;oldid=prev"/>
				<updated>2010-05-25T14:20:56Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:20, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://cppe.ru/index.php?title=C/String/String_Convert&amp;diff=147&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C/String/String_Convert&amp;diff=147&amp;oldid=prev"/>
				<updated>2010-05-25T10:22:19Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Absolute value of long integer: how to use labs==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
  int n, m;&lt;br /&gt;
  n = labs( 65537 );&lt;br /&gt;
  m = labs( -110000 );&lt;br /&gt;
  printf (&amp;quot;n = %d \n&amp;quot;, n);&lt;br /&gt;
  printf (&amp;quot;m = %d\n&amp;quot;, m);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Change string to integer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  char num[80];&lt;br /&gt;
  gets(num);&lt;br /&gt;
  printf(&amp;quot;%d&amp;quot;, abs( atoi( num ) ) ); &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Convert one or more floating-point values into a single string==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Beginning C, Third Edition&lt;br /&gt;
 By Ivor Horton&lt;br /&gt;
 ISBN: 1-59059-253-0&lt;br /&gt;
 Published: Apr 2004&lt;br /&gt;
 Publisher: apress&lt;br /&gt;
*/&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdarg.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
char* to_string(int count, double first, ...);  /* Converts doubles to a string separated by commas */&lt;br /&gt;
char* fp_to_str(double x);                      /* Converts x to a string                           */&lt;br /&gt;
char* int_to_str(int n);                        /* Converts n to a string                           */ &lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
  char *str = NULL;                  /* Pointer to the string of values */&lt;br /&gt;
  double values[] = { 1.245, -3.5, 6.758, 33.399, -1.02 };&lt;br /&gt;
  str = to_string(sizeof values/sizeof(double), values[0], values[1], values[2], values[3], values[4]);&lt;br /&gt;
  printf(&amp;quot;The string of values is:\n%s\n&amp;quot;, str);&lt;br /&gt;
 &lt;br /&gt;
  free(str);                         /* Free memory for string */&lt;br /&gt;
}&lt;br /&gt;
/************&lt;br /&gt;
 * Function to convert one or more floating-point values to a      *&lt;br /&gt;
 * string with the values separated by commas.                     *&lt;br /&gt;
 * This function allocates memory that must be freed by the caller *&lt;br /&gt;
 ************/&lt;br /&gt;
char* to_string(int count, double first, ...)&lt;br /&gt;
{&lt;br /&gt;
  va_list parg = NULL;          /* Pointer to variable argument   */&lt;br /&gt;
  char* str = NULL;             /* Pointer to the joined strings  */&lt;br /&gt;
  char *temp = NULL;            /* Temporary string pointer       */&lt;br /&gt;
  char *value_str = 0;          /* Pointer to a value string      */&lt;br /&gt;
  const char *separator = &amp;quot;,&amp;quot;;  /* Separator in values string     */&lt;br /&gt;
  size_t separator_length = 0;  /* Length of separator string     */&lt;br /&gt;
  size_t length = 0;            /* Length of a string             */&lt;br /&gt;
  int i = 0;                    /* Loop counter                   */&lt;br /&gt;
  separator_length = strlen(separator);&lt;br /&gt;
  va_start(parg,first);         /* Initialize argument pointer */&lt;br /&gt;
  str = fp_to_str(first);       /* convert the first value     */&lt;br /&gt;
  /* Get the remaining arguments, convert them and append to the string */&lt;br /&gt;
  while(--count&amp;gt;0)&lt;br /&gt;
  {&lt;br /&gt;
    value_str = fp_to_str(va_arg(parg, double));    /* Get next argument */&lt;br /&gt;
    length = strlen(str) + strlen(value_str) + separator_length +1;&lt;br /&gt;
    temp = (char*)malloc(length); /* Allocate space for string with argument added */&lt;br /&gt;
    strcpy(temp, str);            /* Copy the old string         */&lt;br /&gt;
    free(str);                    /* Release old memory          */&lt;br /&gt;
    str = temp;                   /* Store new memory address    */&lt;br /&gt;
    temp = NULL;                  /* Reset pointer               */&lt;br /&gt;
    strcat(str,separator);        /* Append separator            */&lt;br /&gt;
    strcat(str,value_str);        /* Append value string         */&lt;br /&gt;
    free(value_str);              /* Release value string memory */&lt;br /&gt;
  }&lt;br /&gt;
  va_end(parg);                   /* Clean up arg pointer        */&lt;br /&gt;
  return str;&lt;br /&gt;
}&lt;br /&gt;
/********************&lt;br /&gt;
 * Converts the floating-point argument to a string.                       *&lt;br /&gt;
 * Result is with two decimal places.                                      *&lt;br /&gt;
 * Memory is allocated to hold the string and must be freed by the caller. *&lt;br /&gt;
 ********************/&lt;br /&gt;
char* fp_to_str(double x)&lt;br /&gt;
{&lt;br /&gt;
  char *str = NULL;                 /* Pointer to string representation     */&lt;br /&gt;
  char *integral = NULL;            /* Pointer to integral part as string   */&lt;br /&gt;
  char *fraction = NULL;            /* Pointer to fractional part as string */&lt;br /&gt;
  size_t length = 0;                /* Total string length required         */&lt;br /&gt;
  integral = int_to_str((int)x);    /* Get integral part as a string with a sign */&lt;br /&gt;
  /* Make x positive */&lt;br /&gt;
  if(x&amp;lt;0)&lt;br /&gt;
    x = -x;&lt;br /&gt;
  /* Get fractional part as a string */&lt;br /&gt;
  fraction = int_to_str((int)(100.0*(x+0.005-(int)x)));&lt;br /&gt;
  length = strlen(integral)+strlen(fraction)+2;  /* Total length including point and terminator */&lt;br /&gt;
  /* Fraction must be two digits so allow for it */&lt;br /&gt;
  if(strlen(fraction)&amp;lt;2)&lt;br /&gt;
    ++length;&lt;br /&gt;
  str = (char*)malloc(length);        /* Allocate memory for total */&lt;br /&gt;
  strcpy(str, integral);              /* Copy the integral part    */&lt;br /&gt;
  strcat(str, &amp;quot;.&amp;quot;);                   /* Append decimal point      */&lt;br /&gt;
  if(strlen(fraction)&amp;lt;2)              /* If fraction less than two digits */&lt;br /&gt;
    strcat(str,&amp;quot;0&amp;quot;);                  /* Append leading zero       */&lt;br /&gt;
  strcat(str, fraction);              /* Append fractional part    */&lt;br /&gt;
  /* Free up memory for parts as strings */&lt;br /&gt;
  free(integral);&lt;br /&gt;
  free(fraction);&lt;br /&gt;
  return str;&lt;br /&gt;
}&lt;br /&gt;
/********************&lt;br /&gt;
 * Converts the integer argument to a string.                              *&lt;br /&gt;
 * Memory is allocated to hold the string and must be freed by the caller. *&lt;br /&gt;
 ********************/&lt;br /&gt;
char* int_to_str(int n)&lt;br /&gt;
{&lt;br /&gt;
  char *str = NULL;                    /* pointer to the string */&lt;br /&gt;
  int length = 1;                      /* Number of characters in string(at least 1 for terminator */&lt;br /&gt;
  int temp = 0;&lt;br /&gt;
  int sign = 1;                        /* Indicates sign of n */&lt;br /&gt;
  /* Check for negative value */&lt;br /&gt;
  if(n&amp;lt;0)&lt;br /&gt;
  {&lt;br /&gt;
    sign = -1;&lt;br /&gt;
    n = -n;&lt;br /&gt;
    ++length;                          /* For the minus character */&lt;br /&gt;
  }&lt;br /&gt;
  /* Increment length by number of digits in n */&lt;br /&gt;
  temp = n;&lt;br /&gt;
  do&lt;br /&gt;
  {&lt;br /&gt;
    ++length;&lt;br /&gt;
  }&lt;br /&gt;
  while((temp /= 10)&amp;gt;0);&lt;br /&gt;
  str = (char*)malloc(length);        /* Allocate space required */&lt;br /&gt;
  if(sign&amp;lt;0)                          /* If it was negative      */&lt;br /&gt;
    str[0] = &amp;quot;-&amp;quot;;                     /* Insert the minus sign   */&lt;br /&gt;
  str[--length] = &amp;quot;\0&amp;quot;;               /* Add the terminator to the end */&lt;br /&gt;
  /* Add the digits starting from the end */&lt;br /&gt;
  do&lt;br /&gt;
  {&lt;br /&gt;
    str[--length] = &amp;quot;0&amp;quot;+n%10;&lt;br /&gt;
  }while((n /= 10) &amp;gt; 0);&lt;br /&gt;
  return str;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Convert string entered to integer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  char num1[80], num2[80];&lt;br /&gt;
  printf(&amp;quot;Enter first: &amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  gets(num1);&lt;br /&gt;
  &lt;br /&gt;
  printf(&amp;quot;Enter second: &amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  gets(num2);&lt;br /&gt;
  &lt;br /&gt;
  printf(&amp;quot;The sum is: %d.&amp;quot;, atoi(num1) + atoi(num2));&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Convert string entered to long==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  char num1[80], num2[80];&lt;br /&gt;
  printf(&amp;quot;Enter first: &amp;quot;);&lt;br /&gt;
  gets(num1);&lt;br /&gt;
  &lt;br /&gt;
  printf(&amp;quot;Enter second: &amp;quot;);&lt;br /&gt;
  gets(num2);&lt;br /&gt;
  &lt;br /&gt;
  printf(&amp;quot;The sum is: %ld.&amp;quot;, atol(num1)+atol(num2));&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Convert string to double: how to use atof ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;math.h&amp;gt;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
  double n, m;&lt;br /&gt;
  double pi = 3.1415926535;&lt;br /&gt;
  char str[256];&lt;br /&gt;
  printf ( &amp;quot;Enter degrees: &amp;quot; );&lt;br /&gt;
  gets ( str );&lt;br /&gt;
  n = atof ( str );&lt;br /&gt;
  m = sin (n * pi / 180);&lt;br /&gt;
  printf ( &amp;quot;sine of %f degrees = %f\n&amp;quot; , n, m );&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Convert string to double: How to use atof: sines calculator==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;math.h&amp;gt;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
  double n, m;&lt;br /&gt;
  double pi=3.1415926535;&lt;br /&gt;
  char   str[256];&lt;br /&gt;
  printf ( &amp;quot;Enter degrees: &amp;quot; );&lt;br /&gt;
  gets ( str );&lt;br /&gt;
  n = atof ( str );&lt;br /&gt;
  m = sin ( n * pi / 180 );&lt;br /&gt;
  printf ( &amp;quot; sine of %f degrees = %f \n&amp;quot; , n, m );&lt;br /&gt;
  &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Convert string to double-precision floating-point value==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;ctype.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  char *end, *start = &amp;quot;Value: 111.1111111&amp;quot;;&lt;br /&gt;
  end = start;&lt;br /&gt;
  while( *start ) {&lt;br /&gt;
    printf(&amp;quot;%f, &amp;quot;, strtod(start, &amp;amp;end));&lt;br /&gt;
    printf(&amp;quot;Remainder: %s\n&amp;quot; ,end);&lt;br /&gt;
    start = end;&lt;br /&gt;
    &lt;br /&gt;
    /* move past the non-digits */&lt;br /&gt;
    &lt;br /&gt;
    while(!isdigit(*start) &amp;amp;&amp;amp; *start) &lt;br /&gt;
        start++;&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Convert string to double-precision floating-point value: how to use strtod==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
  char str[256];&lt;br /&gt;
  char *p;&lt;br /&gt;
  double dbl;&lt;br /&gt;
  &lt;br /&gt;
  printf (&amp;quot;Enter a floating-point value: &amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  gets (str);&lt;br /&gt;
  &lt;br /&gt;
  dbl = strtod (str, &amp;amp;p);&lt;br /&gt;
  &lt;br /&gt;
  printf (&amp;quot;Value entered: %lf. Its square: %lf\n&amp;quot;, dbl, dbl * dbl);&lt;br /&gt;
  &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Convert string to int, double and long==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
  int i;&lt;br /&gt;
  double d;&lt;br /&gt;
  long l;&lt;br /&gt;
  i = atoi(&amp;quot;1&amp;quot;);&lt;br /&gt;
  l = atol(&amp;quot;11111111&amp;quot;);&lt;br /&gt;
  d = atof(&amp;quot;11111.11111&amp;quot;);&lt;br /&gt;
  printf(&amp;quot;%d %ld %f&amp;quot;, i, l, d);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Convert string to integer: atoi==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
  int i;&lt;br /&gt;
  char str[256];&lt;br /&gt;
  printf (&amp;quot;Enter a number: &amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  gets ( str );&lt;br /&gt;
  &lt;br /&gt;
  i = atoi ( str );&lt;br /&gt;
  &lt;br /&gt;
  printf (&amp;quot;i =  %d, its double = %d&amp;quot;, i, i * 2 );&lt;br /&gt;
  &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Convert string to long : atol==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
  int i;&lt;br /&gt;
  char str[256];&lt;br /&gt;
  printf (&amp;quot;Enter a long number: &amp;quot;);&lt;br /&gt;
  gets ( str );&lt;br /&gt;
  i = atol ( str );&lt;br /&gt;
  printf (&amp;quot;i = %d, its double = %d&amp;quot;, i, i * 2 );&lt;br /&gt;
 &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Convert string to long integer: how to use strtol==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
  char str[256];&lt;br /&gt;
  char *p;&lt;br /&gt;
  long l;&lt;br /&gt;
  &lt;br /&gt;
  printf (&amp;quot;Enter an long value: &amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  gets (str);&lt;br /&gt;
  l = strtol (str, &amp;amp;p, 0);&lt;br /&gt;
  &lt;br /&gt;
  printf (&amp;quot;Value entered: %ld. Its double: %ld\n&amp;quot;, l, l * 2);&lt;br /&gt;
  &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Convert string to unsigned long integer: how to use strtoul==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
  char str[256];&lt;br /&gt;
  char *p;&lt;br /&gt;
  unsigned long ul;&lt;br /&gt;
  &lt;br /&gt;
  printf (&amp;quot;Enter an integer value: &amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  gets (str);&lt;br /&gt;
  &lt;br /&gt;
  ul = strtoul (str, &amp;amp;p, 0);&lt;br /&gt;
  &lt;br /&gt;
  printf (&amp;quot;Value entered: %lu. Its double: %lu\n&amp;quot;, ul, ul * 2);&lt;br /&gt;
  &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Join array of strings into a single string==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Beginning C, Third Edition&lt;br /&gt;
 By Ivor Horton&lt;br /&gt;
 ISBN: 1-59059-253-0&lt;br /&gt;
 Published: Apr 2004&lt;br /&gt;
 Publisher: apress&lt;br /&gt;
*/&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;ctype.h&amp;gt;&lt;br /&gt;
#define MAX_STRINGS 100                         /* Maximum string count                        */&lt;br /&gt;
#define BUFFER_SIZE 50                          /* Initial input buffer size                   */&lt;br /&gt;
char* join_strings(char *strings[], int count); /* Joins array of strings into a single string */&lt;br /&gt;
char* read_string();                            /* Reads a string from the keyboard            */&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
  char *pStrings[MAX_STRINGS];       /* Array of pointers to strings */&lt;br /&gt;
  char *joined_strings = NULL;       /* Pointer to the joined string */&lt;br /&gt;
  int count = 0;                     /* Number of strings entered    */&lt;br /&gt;
  char answer = &amp;quot;y&amp;quot;;                 /* Confirms more input          */&lt;br /&gt;
  char terminator = &amp;quot;*&amp;quot;;             /* Terminator for string entry  */&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  /* Read the strings */&lt;br /&gt;
  while(count&amp;lt;MAX_STRINGS &amp;amp;&amp;amp; tolower(answer)==&amp;quot;y&amp;quot;)&lt;br /&gt;
  {&lt;br /&gt;
    printf(&amp;quot;Enter a string:\n&amp;quot;);&lt;br /&gt;
    pStrings[count++] = read_string(terminator);&lt;br /&gt;
    printf(&amp;quot;Do you want to enter another: &amp;quot;);&lt;br /&gt;
    scanf(&amp;quot; %c&amp;quot;, &amp;amp;answer);&lt;br /&gt;
    fflush(stdin);                     /* Lose newline following character entry */&lt;br /&gt;
  }&lt;br /&gt;
  joined_strings = join_strings(pStrings, count); &lt;br /&gt;
  printf(&amp;quot;\nHere are the strings as a single string:\n%s\n&amp;quot;, joined_strings);&lt;br /&gt;
  free(joined_strings);                /* Free memory for joined strings   */&lt;br /&gt;
  for(i = 0 ; i&amp;lt;count ; i++)           /* Free memory for original strings */&lt;br /&gt;
    free(pStrings[i]);&lt;br /&gt;
}&lt;br /&gt;
/************&lt;br /&gt;
 * Function to join an array of strings                            *&lt;br /&gt;
 * this function allocates memory that must be freed by the caller *&lt;br /&gt;
 ************/&lt;br /&gt;
char* join_strings(char *strings[], int count)&lt;br /&gt;
{&lt;br /&gt;
  char* str = NULL;             /* Pointer to the joined strings  */&lt;br /&gt;
  size_t total_length = 0;      /* Total length of joined strings */&lt;br /&gt;
  size_t length = 0;            /* Length of a string             */&lt;br /&gt;
  int i = 0;                    /* Loop counter                   */&lt;br /&gt;
  /* Find total length of joined strings */&lt;br /&gt;
  for(i = 0 ; i&amp;lt;count ; i++)&lt;br /&gt;
  {&lt;br /&gt;
    total_length += strlen(strings[i]);&lt;br /&gt;
    if(strings[i][strlen(strings[i])-1] != &amp;quot;\n&amp;quot;)&lt;br /&gt;
      ++total_length; /* For newline to be added */&lt;br /&gt;
  }&lt;br /&gt;
  ++total_length;     /* For joined string terminator */&lt;br /&gt;
  str = (char*)malloc(total_length);  /* Allocate memory for joined strings */&lt;br /&gt;
  str[0] = &amp;quot;\0&amp;quot;;                      /* Empty string we can append to      */&lt;br /&gt;
  /* Append all the strings */&lt;br /&gt;
  for(i = 0 ; i&amp;lt;count ; i++)&lt;br /&gt;
  {&lt;br /&gt;
    strcat(str, strings[i]);&lt;br /&gt;
    length = strlen(str);&lt;br /&gt;
    /* Check if we need to insert newline */&lt;br /&gt;
    if(str[length-1] != &amp;quot;\n&amp;quot;)&lt;br /&gt;
    {&lt;br /&gt;
      str[length] = &amp;quot;\n&amp;quot;;             /* Append a newline       */&lt;br /&gt;
      str[length+1] = &amp;quot;\0&amp;quot;;           /* followed by terminator */&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  return str;&lt;br /&gt;
}&lt;br /&gt;
/********************&lt;br /&gt;
 * Reads a string of any length.                                           *&lt;br /&gt;
 * The string is terminated by the chracter passed as the argument.        *&lt;br /&gt;
 * Memory is allocated to hold the string and must be freed by the caller. *&lt;br /&gt;
 ********************/&lt;br /&gt;
char* read_string(char terminator)&lt;br /&gt;
{&lt;br /&gt;
  char *buffer = NULL;            /* Pointer to the input buffer */&lt;br /&gt;
  int buffersize = BUFFER_SIZE;   /* Current buffer capacity     */&lt;br /&gt;
  int length = 0;                 /* String length               */&lt;br /&gt;
  char *temp = NULL;              /* Temporary buffer pointer    */&lt;br /&gt;
  int i = 0;                      /* Loop counter                */&lt;br /&gt;
  buffer = (char*)malloc(BUFFER_SIZE);  /* Initial buffer */&lt;br /&gt;
  /* Read the string character by character */&lt;br /&gt;
  for(;;)&lt;br /&gt;
  {&lt;br /&gt;
    /* Check for string terminator */ &lt;br /&gt;
    if((buffer[length] = getchar()) == terminator)&lt;br /&gt;
      break;&lt;br /&gt;
    else&lt;br /&gt;
      ++length;&lt;br /&gt;
    /* Check for buffer overflow */&lt;br /&gt;
    if(length == buffersize)&lt;br /&gt;
    {&lt;br /&gt;
      buffersize += BUFFER_SIZE;          /* Increase buffer size */&lt;br /&gt;
      temp = (char*)malloc(buffersize);   /* Allocate new buffer  */&lt;br /&gt;
      /* Copy characters from old buffer to new */&lt;br /&gt;
      for(i = 0 ; i&amp;lt;length ; i++)&lt;br /&gt;
        temp[i] = buffer[i];&lt;br /&gt;
      free(buffer);                       /* Free memory for old buffer */&lt;br /&gt;
      buffer = temp;                      /* Store new buffer address   */&lt;br /&gt;
      temp = NULL;                        /* Rest temp pointer          */&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  buffer[length] = &amp;quot;\0&amp;quot;;                  /* Append string terminator                  */&lt;br /&gt;
  temp = (char*)malloc(length+1);         /* Allocate exact memory required for string */&lt;br /&gt;
  strcpy(temp, buffer);                   /* Copy the string       */&lt;br /&gt;
  free(buffer);                           /* Free the buffer memory */&lt;br /&gt;
  return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Print formatted data to a string: how to use sprintf==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
  char buffer [50];&lt;br /&gt;
  int n, a = 50, b = 37;&lt;br /&gt;
  &lt;br /&gt;
  n = sprintf (buffer, &amp;quot;%d plus %d is %d&amp;quot;, a, b, a + b);&lt;br /&gt;
  &lt;br /&gt;
  printf (&amp;quot;[%s] is a %d chars string\n&amp;quot;, buffer, n);&lt;br /&gt;
  &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>