Page 1 of 1

[Resolved] Get a number from a random string?

PostPosted: Wed Feb 19, 2014 3:46 pm
by bat78
Because of some reason Game-Editor do not support strtok with is newer then sscanf and as i remember it is a part of C89 standart liberally. Anyway.. lets say user input string in textbox like:
Code: Select all
I am 16 years old.

And a neutral text actor prints 16.

Code: Select all
1 + 1 = 2.

And the neutral actor prints 112.

Maybe something like:

Code: Select all
char * strton ( char str[512] )
{
   char * str_output;
   int n;
   
   if (str[n] == '0' ||
   str[n] == '1' ||
   str[n] == '2' ||
   str[n] == '3' ||
   str[n] == '4' ||
   str[n] == '5' ||
   str[n] == '6' ||
   str[n] == '7' ||
   str[n] == '8' ||
   str[n] == '9') { sprintf(str_output, "%c", str[n]); }
             else { n++; }
   return str_output;
}

Re: [Resolved] Get a number from a random string?

PostPosted: Wed Jul 16, 2014 7:56 pm
by Bee-Ant
Take a look at this ascii table: http://www.asciitable.com
Number 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 :)

Re: [Resolved] Get a number from a random string?

PostPosted: Thu Jul 17, 2014 9:36 am
by bat78
Bee-Ant wrote:Take a look at this ascii table: http://www.asciitable.com
Number 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.

Re: [Resolved] Get a number from a random string?

PostPosted: Thu Jul 17, 2014 11:21 am
by Bee-Ant
bat78 wrote:However.. i solved it by myself already and it was pretty much long time ago. x)

Haha... actually I was just hanging around the forum and answering "not answered" post :D
Good to see that you already solved it by yourself.

Anyway, why do you need such function?

Re: [Resolved] Get a number from a random string?

PostPosted: Thu Jul 17, 2014 11:57 am
by bat78
Bee-Ant wrote:
bat78 wrote:However.. i solved it by myself already and it was pretty much long time ago. x)

Haha... actually I was just hanging around the forum and answering "not answered" post :D
Good to see that you already solved it by yourself.

Anyway, why do you need such function?


I already forgot. lol.
But function such as strtoint is extremely useful i say. atoi returns string digit characters as an integer only if the entire string contains only digit characters.