Bee-Ant wrote:Take a look at this ascii table:
http://www.asciitable.comNumber starts from 48 to 57, so here's the function
- Code: Select all
int GetNumber(char str[256])
{
int i,j,result;
char tmp[256],part[2];
for(i=0;i<srtlen(str);i++)
{
j=(int)str[i];
if(j>=48&&j<=57)
{
sprintf(part,"%s",&j);
strcat(tmp,part);
}
}
result=atoi(tmp);
return result;
}
Usage:
- Code: Select all
int Number=GetNumber("You're 16 years old");
Will give you 16
Bee-ant!
Hey. Thanks for the replay and the code example. However.. i solved it by myself already and it was pretty much long time ago. x)
From my place of view now it looks quite as a retarded question that destroys my dignity. But i hope other people find that solution of yours and apply it.
- Code: Select all
int strtoint ( char source[] )
{
int multiplier = 1;
int i, result = 0;
for (i=strlen(source)-1; i>=0; i--)
{
if(source[i] >= '0' && source[i] <= '9')
{
result = result + ((source[i]-'0') * multiplier);
multiplier *= 10;
}
}
return result;
}
I found useful to count the unique characters. It could be used to count the unique digit characters:
- Code: Select all
int CountUniqueCharacters(char* str){
int count = 0;
int i, j;
for (i = 0; i < strlen(str); i++){
bool appears = false;
for (j = 0; j < i; j++){
if (str[j] == str[i]){
appears = true;
break;
}
}
if (!appears){
count++;
}
}
return count;
}
Note that you have to add the boolean type here. It looks clear.