c - How to convert int stored in char array to int variable for calculations? -
i need go through each location of char array there int stored, , need utilize each number calculations.
i have
char num1[a]; char num2[b];
basically, need add together num1[1]+num2[1]
, num1[2]+num2[2]
, on. how can convert each character location int
calculations? in advance
your question bit unclear.
if want add together int
s in char
array,use isdigit()
ctype.h
check if digit , add together variable sum
accordingly
int i,sum=0,len=strlen(num1); for( i=0 ; i<len ; i++ ) { if(isdigit(num1[i]) sum=sum+(num1[i]-'0'); }
or else if sure array contain numbers,remove if
in above code
if want add together each integer of 2 different arrays,use
sum=(num1[i]-'0')+(num2[i]-'0');
where sum
int
variable initialized zero,num1
, num2
char
arrays , i
,the index of array want add.
c
No comments:
Post a Comment