Search
 
SCRIPT & CODE EXAMPLE
 

C

taking input and converting it to a string in 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]='';
    ///to know the value of i at this point decomment me:
    //printf("%li 
", 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 
", i);

// printing the whole string
    printf("%s 
", 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 
", str[10]);

//printing the whole string using a for loop
    str[k+1]='';
    for (int j =-1; j<k; j++)
    {
    printf("%c", str[j+1]);
    }
//using pointers
	char *c = "9876543210";

	str[k+1]='';
    for (int l =-1; l<k+1; l++)
    {
    printf("%c", *(c+l) );
    }

printf("
");
}

//you should see 4 sets of 9876543210
Comment

PREVIOUS NEXT
Code Example
C :: changing data type in one line c program 
C :: change no_turbo 
C :: nc,manc 
C :: Print fabionci with fork in C 
C :: c Modulo 10^9+7 (1000000007) 
C :: Calculate the area of a circle and modify the same program to calculate the volume of a cylinder given its radius and height. 
C :: largest value in u32 
C :: batteries included 
C :: difference between %f and %lf 
C :: online c compiler with mpi 
C :: passing an array of unspecified number of variables to a function 
C :: i765 OPT filing fees october 2 
C :: write to file in c programming 
C :: how to declare a structure in c 
C :: c code to algorithm converter online 
C :: c triangle check if triangle is 90 degrees 
Dart :: flutter rounded bottom sheet 
Dart :: flutter get current date 
Dart :: flutter width infinity 
Dart :: change padding in text field flutter 
Dart :: flutter run code after build 
Dart :: flutter showsnackbar 
Dart :: trailing flutter with 2 icons flutter 
Dart :: flutter lock orientation 
Dart :: flutter compare dates 
Dart :: convert string to float .0 dart 
Dart :: flutter icon tap 
Dart :: dartlang group array by key 
Dart :: pass function as parameter in flutter 
Dart :: flutter get package command 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =