guys .. how can u forget
 
sscanf(string,"%d", &intvar);
this is the simplest ... u can get float as well using %f.
 
koul
--- On Fri, 12/6/09, siddhiruhi011 <[email protected]> wrote:


From: siddhiruhi011 <[email protected]>
Subject: [c-prog] Re: char type string to int type variable.
To: [email protected]
Date: Friday, 12 June, 2009, 8:05 PM








--- In c-p...@yahoogroups. com, "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

















      Explore and discover exciting holidays and getaways with Yahoo! India 
Travel http://in.travel.yahoo.com/

[Non-text portions of this message have been removed]

Reply via email to