Originally Posted by
mikko3000
Please help me.
Program has the following definition:
char string[] = "1234";
How do i make a program which converts numbers given in a string to integer without using the "atoi" -function?
My greatest problem is how to compare a single character in a string.
For example
if (string[0]=="1") // this naturally is wrong
Thanks in advance.
Mikko, Finland
hi mikko,
In C we can't compare two strings using relational operators.But using strcmp() we can compare two strings. Instead of using if (string[0]=="1") you just use if(string[0]='1') to compate two chracters.you can convert a character in to number by subtracting character 'zero' or integer 48.
for(I=0;string[i]!='\0';i++)
{
int j=string[i]-'0';
num=num*10+j;
}