1

I'm trying to convert a string containing a number. The number is thousand separated with a dot. I've made a simple solution with sscanf(input, "%i.%i", &first, %second); return first * 1000 + second;

This works alright, but it has its limitations as it only works for numbers below 1.000.000. I've tried making this recursive, but my program wont compile. Maybe one of you guys, can see whats wrong.

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

char* concat(const char *s1, const char *s2) {
    char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}

char *convert_spectators( char *input)                                 //
{
    char fir[3], sec[100];
    int scan_res = sscanf(input, "%3[0123456789]%*[^0123456789]%s", fir, sec);

    if (scan_res == 2) {
        return concat(fir, convert_spectators(sec));
    } else if (scan_res == 1) {
        return fir;
    }

}

int main() {
    printf("Result: %i\n", atoi(convert_spectators("123.456.789")));
    return 1;
} 
3
  • I don't understand. What is the expected output for "123.456.789"? And what are you getting? Commented Nov 29, 2016 at 18:13
  • 1
    Btw, from convert_spectators() you are returning "fir", which is a local array. It is wrong. Commented Nov 29, 2016 at 18:16
  • ... and not all control paths return a value anyway. Commented Nov 29, 2016 at 18:20

2 Answers 2

1

from convert_spectators() you are returning "fir", which is a local array. It is wrong. I modified your code and passing fir and sec from calling function.

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

char* concat(const char *s1, const char *s2) {
    char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}

char *convert_spectators( char *input, char *fir, char *sec)                                 //
{
    int scan_res = sscanf(input, "%3[0123456789]%*[^0123456789]%s", fir, sec);

    if (scan_res == 2) {
    char firTemp[4]   = "";
    char secTemp[100] = "";
        return concat(fir, convert_spectators(sec, firTemp, secTemp));
    } else if (scan_res == 1) {
        return fir;
    } else {
        printf("Something wrong, scan_res[%d]\n", scan_res);
        return NULL;
    }

}

int main() {
    char fir[4]   = "";
    char sec[100] = "";

    printf("Result: %i\n", atoi(convert_spectators("123.456.789", fir, sec)));
    return 1;
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's exactly what i was looking for! It's kinda difficult for me to follow the fir, sec, firTemp and secTemp arround like this, but it works! Thanks a lot! +1
You should not return address of local variable from a function as it's scope is limited to that function. In case you want to return, you have to allocate memory for that and free it sometime. Instead of that, you can pass address of local variable from calling function as I did here so that you don't have to worry about alloc() and free(). But you can not use this technique if your memory requirement is very large. Read this for more info about variable scope: c4learn.com/c-programming/c-local-variable-scope
0

This can be accomplished without scanf

int convert(char *s, int n) {
    if (!*s || n >= 1000000000)
        return n;

    if (*s >= '0' && *s <= '9')
        n = n * 10 + *s - 48;

    return convert(s+1, n);
}

int main() {
    char *str = "123.456.789";
    printf("%d\n", convert(str, 0));
}

This will just parse through the string, and skip any characters that aren't digits. And stop at the end of the string or when result overflows 32 bits.

As mentioned, your way of concatenating each substring and converting to an integer is fine, but you are returning a local array in your function convert_spectators(). You need to declare that array locally before you call the function, or make it global.

2 Comments

This is part of my programming course, and recursive programming is one of the elements i would like to incooperate into my solution, but thanks for the answer! :)
This uses recursion too, it just simplifies the solution instead of converting to a string without '.', just append each digit until the end of the string. Much more efficient

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.