-2

I'm trying to take a part of a string and put it into a int. For example:

char str[] = "bla bla bla 98";
int a;  

And I want to put the 98 in a. I can use only string.h.

edit:

I wrote a function that separate the number from the string,
so "the new problem" was to cast array of chars to int, but the array include only numbers.
for example, if I use the str above, after using the function str = "98", and now is much easier to solve.

puhs
  • 87
  • 8
  • 1
    You've got to put a little bit of effort into this, SO is not a code writing service, Try searching for "string.h extract int from string" or check out this post in SO https://stackoverflow.com/q/13399594/1690217 – Chris Schaller Dec 10 '17 at 22:23
  • 1
    What about `"-+123"`, `"+-123"`, `"0 abc 1"`, `"abc"`, `"123"`, `"- abc 1"`, `"123 456"`, and many other cases? – chux - Reinstate Monica Dec 10 '17 at 22:24
  • If you can't use either `` or ``, your program isn't going to be much use. Presumably, the "only ``" rule mainly means "no, you can't use `atoi()` or `strtol()` from ``, nor `scanf()` and family", but you are in fact allowed to use standard I/O so you can see what your code produces. With that said, look at `strcspn()` from ``. Are you allowed to use `isdigit()` from ``? Oh well, … – Jonathan Leffler Dec 10 '17 at 22:30
  • You may not even need `string.h` if the input can be expected to match the example format. What if input is `"bla bla bla 98.1"`? Should `a` get a value of `98`? Should an input error be reported? A better description of problem parameters would be helpful for a good answer. – ad absurdum Dec 10 '17 at 22:31

1 Answers1

2

This simple program will demonstrates how to get first encountered integer number in the string str[].

Function get_first_encountered_number(char *str, int *number); can be reused to hunt for more numbers. Negative integer numbers are also recognized.

#include <stdio.h>
#include <string.h>
int my_atoi(char *str)
{
    int result = 0; 

    for (int i = 0; str[i] != '\0'; ++i)
        result = result*10 + str[i] - '0';

    return result;
}

char * get_first_encountered_number(char *str, int *number)
{
    int i;

    for (i=0; str[i]!=0; i++)
    {

        if ( (str[i] < '0') || (str[i] > '9'))
            continue;

        if( (i>0) && (str[i-1] == '-'))
           *number = -my_atoi(&str[i]);
         else  
           *number = my_atoi(&str[i]);        

         break;
     }

     return &str[i];
 }

int main()
{
    char str[] = "bla bla bla 98";
    char str1[] = "bla bla bla -98";
    int a;

    get_first_encountered_number(str,&a);
    printf("number= %d\n",a);

    get_first_encountered_number(str1,&a);
    printf("number= %d\n",a);

    return 0;
}

OUTPUT:

number= 98
number= -98
sg7
  • 6,108
  • 2
  • 32
  • 40
  • @DavidBowling Thanks! Typo corrected. Regarding `-98` you are right again! However, the OP is not precise what he wants and did not show any code. My code works for the presented input but I do not attempt to cover all possible cases. I hope that the code presented `as is` may be a good starting point for the OP to develop his version. – sg7 Dec 11 '17 at 00:11
  • 1
    @DavidBowling I have added recognition of the integer negative numbers. Thanks! – sg7 Dec 11 '17 at 00:37
  • you should make `my_atoi` stop when it finds a character that is not a digit. – chqrlie Aug 11 '19 at 13:25