--- In [email protected], "piyush_4love_4ever" <piyush_4love_4e...@...>
wrote:
>
> can we chage a char type string into a int type variable.
> pls show how is it possible ?
> suppose i define
> char *str;
> gets(str) ; // suppose we get two int in str
> now i want to store these two int into a single int type variable.
> how is it possible??
>
we can use library functions to convert character string into integer variable
but we can write our own function like the following one which is compiled
using Turbo C
int main()
{
char *str;
int cnt;
clrscr();
gets(str);
printf("%s\n",str);
cnt=chng(str,5);
printf("%d\n",cnt);
getch();
return 0;
}
int chng(char *tmp,int no)
{
char ch1;
int j,cng=0,cngs=0;
for(j=0;j<no;j++)
{
ch1=tmp[j];
cng=(int)ch1;
cng&=0x0f;
cngs+=cng;
if(j!=no-1)
cngs*=10;
}
return cngs;
}
if there is some problem please let me know. thanx
from BISD Labs, New Delhi-46