Solved

copy a substring and convert it into int

Posted on 2004-10-19
28
271 Views
Last Modified: 2010-04-15
hi,

I got a string and i would like to copy a part of it and turn it into integer.

e.g.

temp_line="#version: 112233"

what i need is retrieve the 11, 22 and 33
and return them as integer.


thanx :p
0
Comment
Question by:Knightley
  • 9
  • 7
  • 5
  • +2
28 Comments
 
LVL 5

Expert Comment

by:van_dy
ID: 12345929
Here is a dirty one just suitable for above kindof strings;

 #include <stdlib.h>
#include <stdio.h>

void getintegers(string, num1, num2, num3, index)
                  char *string;
                  int *num1, *num2, *num3, index;
{
                  char *p = string + index;
                  int mynumber = atoi(p);
                   *num1 = mynumber / 10000;
                   mynumber %= 10000;
                   *num2 = mynumber / 100;
               mynumber %= 100;
                   *num3 = mynumber;
                    return;
}

int main()
{
      int num1, num2, num3;
      char *p = "#version: 112233";
        getintegers(p, &num1, &num2, &num3, 9);
      printf("%d %d %d\n", num1, num2, num3);
      return 0;
}
0
 
LVL 1

Author Comment

by:Knightley
ID: 12345989
ok, i see what u mean, could i write the code like this:

int version;
char *temp_line="#version: 112233"

version= temp_line[10]%100000+
              temp_line[11]%10000+
              temp_line[12]%1000+
              temp_line[13]%100+
              temp_line[14]%10+
              temp_line[15];

and there is no problem that temp_line[i] is a char and version is a int???
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12346035
>temp_line="#version: 112233"
>i need is retrieve the 11, 22 and 33
>and return them as integer.
What is the criteria for scanning this string?
Should it start taking the character next to a "space"!?
Should it take 2 character at a time and convert it to int?
So, in the example you have shown, it has to convert it into 3 integers!?

If you can specify these requirements properly, then you may get a better code.
Otherwise, your code may not work for all the cases that might arise later!

-ssnkumar
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12346045
Hi Knightley,
Close. What you mean is probably:

version= (temp_line[10]-'0')*100000+
         (temp_line[11]-'0')*10000+
         (temp_line[12]-'0')*1000+
         (temp_line[13]-'0')*100+
         (temp_line[14]-'0')*10+
         (temp_line[15]-'0');


Cheers!

Stefan
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12346129
Hi Knightly,
         yea u get the point, actually as ssnkumar pointed out, there should be
a crteria to scan a string, like is it always going to begine with "#version: ", or
is the version number going to be broken up always in sets of 2 digits, as we did
here.  the actual algorithm u would implement to extract the numbers would depend
upon these details. hard coding magic numbers like we do in the call to getinegers(), [index = 9]
isnt a clean programming style.

hope this helps,
van_dy
0
 
LVL 45

Accepted Solution

by:
Kdo earned 95 total points
ID: 12346161
Hi Knightley,

Brute force works just fine, but C has a functions to do just what you want.  

char *Str = "112233";
int    x, y, z;

  sscanf (Str, "%2d%2d%2d", &x, &y, &z);

The format can include all kinds of "skip" leading text, too.


You originally said that you want to "copy it and turn in into integer".  Copying it isn't necessary for converting it, but you can always copy the string (or any part of it) with other C functions if that's your wish.


Kent

0
 
LVL 1

Author Comment

by:Knightley
ID: 12346247
stefan:
temp_line[10]-'0' will convert the "char" into a "int"?

van_dy:
yes, the "#version" and the number format always stay the same

Kent:
how do i change the sscanf function if my
char *Str="#version: 112233"

there is a space character behind "#version:" but i think this is not relevant?
0
 
LVL 1

Author Comment

by:Knightley
ID: 12346318
actually i want to return the 11,22,33 as three integers
from a function.

Could c handle a array of integers?

if not i got to return 112233 as a single int, and then
divide it into 3 seperate integers.
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12346360
>Could c handle a array of integers?
YES and NO!
Actually you cannot return arrays. That's why NO.
But, you can simulate the same behaviour using pointers. That's why YES!

Then the code would change to:

int *func(char *str)
{
    int *int_arr = (int *) malloc(3 * sizeof(int));

    sscanf(str, "%2d%2d%2d", &int_arr[0], &int_arr[1], &int_arr[2]);

     return int_arr;
}

-ssnkumar
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12346363

Hi Knightley,

There are lots of ways to skip text.  The simplest is to put the exact text into the format string.


char *Str ="#version: 112233";
int    x, y, z;

  sscanf (Str, "#version: %2d%2d%2d", &x, &y, &z);


More generic skip operations are also availabe that simply say "skip all alphabetic text".


Kent
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12346372
if your string is always going to contain 3 integers, each of 2 digits (xxyyzz)
then kent's solution is undoubtedly the best way to extract these;

char *Str = "#version: 112233";
int ver[3];
sscanf(Str + 10, "%2d%2d%2d", ver, ver + 1, ver + 2);

now the ver[] array will hold those integers.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12346462
Knightley,
> temp_line[10]-'0' will convert the "char" into a "int"?

Sort of. In ASCII, numbers start with 48='0', 49='1', and so on. It's converting a character digit into the 0-9 range.

But don't use this, use a solution which is more sensitive to faulty input.
0
 
LVL 1

Author Comment

by:Knightley
ID: 12346470
actually the format is "#version: 1122233"

so i need something like this?

char *Str = "#version: 1122233";
int ver[3];
sscanf(Str + 10, "%2d%3d%2d", ver, ver + 1, ver + 2);
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 45

Expert Comment

by:Kdo
ID: 12346488

It's a bit unusual to list the target variables (ver, ver+1, vet+2) that way, but it should work just fine!  I like it.  :)

Kent
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12346516
Hi Knightly,
     lol, well tell us first is it also possible to have following formats?
"#version: 1122333"      u need  11 22 333 ?
"#version: 1112233"      u need   111 22 33 ?
"#version:  111222333"  u need   111 222 333 ?
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12346534
in that case, we might need to go back to using the first method
0
 
LVL 1

Author Comment

by:Knightley
ID: 12346566
:p

"#version: " is fixed, it coud be other text, like "#version_of_a_lousy_phone_OS: ".
But once set, it will not be changed.

the digits' format are alike, i do not know if it is 112233 or 11222233, but i know they are
together without spaces.

0
 
LVL 1

Author Comment

by:Knightley
ID: 12346622
ssnkumar:

is your function like this

int *check_version()
{
    char *word="#version: 112233";

    int *int_arr = (int *) malloc(3 * sizeof(int));

    sscanf(str, "%2d%2d%2d", &int_arr[0], &int_arr[1], &int_arr[2]);

     return int_arr;
}
0
 
LVL 1

Author Comment

by:Knightley
ID: 12346646
ahhh, i checkd with my boss, the "#version" is actually:

"#define FIRMWARE_VERSION_NUMBER "

the digits format is:

"1122233"
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12346695

>> ahhh, i checkd with my boss.

Foolish.  But in this case, informative.  :)


Kent

------------------------------------------
Those who can, do.
Those who can not, teach.
Those who can not teach, manage!
------------------------------------------
0
 
LVL 1

Author Comment

by:Knightley
ID: 12346887
i don't know where the files which contain
the "#define FIRMWARE_VERSION_NUMBER 1122233" string
are located on the server.

so i have to ask.

k
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12347036
Well did this just for the heck of it.
try running this.
----------------------------
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>

void getintegers(string, arrayint, size)
      char *string;
      int *arrayint;
      int *size;
{
      int index = 0;
      int index2;
      char ch1, ch2;
      while(!isdigit(ch1 = string[index]))
            index++;

      while(string[index]){
            arrayint[*size] = ch1 - '0';
            index2 = index + 1;
            while((ch2 = string[index2]) == ch1){
                  arrayint[*size] *= 10;
                  arrayint[*size] += ch2 - '0';
                  index2++;
            }
            ch1 = ch2;
            *size += 1;
            index = index2;
      }
}

int main()
{
      char *String = "Funny_String!@#@$#: 11133555999";
      int arrint[10];
      int size = 0;
      int i;
      getintegers(String, arrint, &size);

      for(i = 0; i < size; i++)
            printf("%d\n", arrint[i]);

      return 0;
}

:D
0
 
LVL 1

Author Comment

by:Knightley
ID: 12347141
:D  Funny_String!@#@$#: 11133555999
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12347204
yea u can just put anything there and have ur numbers
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12347535

The solution can still be reduced to a single line:

  sscanf (Str, "%*s %02d%03d%02d", ver, ver + 1, ver + 2);



Kent
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12354767
>ssnkumar
>is your function like this

YES.
But, sscanf part is just to show how to use it.
You can take the more reformed one of kdo.

Still we can have a general solution, only if you know the patterns of version numbers that can come in.
What about:
"#version:  123346"
How should this be scanned. Will it be 3 numbers (12, 33 and 46) or 5 numbers (1, 2, 33, 4 and 6)?

-ssnkumar
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now