Ввод вход и преобразование его в строку в C
#include <stdio.h>
int main(void)
{
//set a to a user input not longer than your declared array length
long a=9876543210,i=0;
/* str will hold the result which is the array
k is for the for loop*/
int k=0;
//set your array length properly this is equivallent to malloc
char str[20]= "";
//b is used for taking the length of the number a
int b=a;
// first we need to see the length of the number a
while(b>=10)
{
b=b/10;
i++;
//setting K for i for the for loop do not include if not using the for loop
k=i;
}
/* the length of the number a will be stored in variable i
we set the end of the string str as we know the length needed*/
str[i+1]='\0';
///to know the value of i at this point decomment me:
//printf("%li \n", i);
/* the while loop below will store the digit from the end of str to the
the beginning */
while(i>=0)
{
str[i]=a%10+48;
a=a/10;
i--;
}
//to know the value of i at this point decomment me:
//printf("%li \n", i);
// printing the whole string
printf("%s \n", str);
// printing the single value of the created string
printf("%c", str[0]);
printf("%c", str[1]);
printf("%c", str[2]);
printf("%c", str[3]);
printf("%c", str[4]);
printf("%c", str[5]);
printf("%c", str[6]);
printf("%c", str[7]);
printf("%c", str[8]);
printf("%c", str[9]);
printf("%c \n", str[10]);
//printing the whole string using a for loop
str[k+1]='\0';
for (int j =-1; j<k; j++)
{
printf("%c", str[j+1]);
}
//using pointers
char *c = "9876543210";
str[k+1]='\0';
for (int l =-1; l<k+1; l++)
{
printf("%c", *(c+l) );
}
printf("\n");
}
//you should see 4 sets of 9876543210
Alejandro Ferrazzini