Hello
i'm trying to build this bit of code:
i get an input (for example: "1 2 45 4 67 3") as string
and i need to put these numbers into an array,
if you can help me out, i'll be grateful.
thank you very much
Hello
i'm trying to build this bit of code:
i get an input (for example: "1 2 45 4 67 3") as string
and i need to put these numbers into an array,
if you can help me out, i'll be grateful.
thank you very much
i'm not allowed to use:
pointers, functions, etc.
i can use strlen,strcpy,strcmp
Well, that just makes it more difficult, but the idea is the same.
You would have to walk through the string to find the spaces and either truncate the string (may be a little over course) or copy it into a new array with strcpy.
Then you would simply have to walk through that new string and translate each char into the appropriate integer and apply a little math.
You will need to use (either your own or a standard function [1]) that converts from string to integer, no matter what you do.
As Elysia indicated, you would have to walk through the string and find the separator, then find the next separator (or end of string), and copy the portion of string out, then convert that string to an integer.
[1] Ok, so we don't HAVE to make it a function - it could be code written inline in the main function - but that would not be good coding style, in my opinion - but it would remove the need to use a function if that's the requirement you have.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
unfortuantley, it's an assigment for Uni., so i don't decide what i can use,
but never mind, i managed to do it anyways.... thank you for your help.
Maybe something like this...
I think there is a simple algorithm using strtok... but I still don't understand how to use it...Code:const wchar_t *input = L"1 2 45 4 67 3"; int *buffer = NULL; //the result saved here int count = 0; //length of the buffer if((buffer = malloc(sizeof(int))) == NULL) //actually, statements like this could be exit(EXIT_FAILURE); //defined as a macro if you wish... size_t i; wchar_t *temp = NULL; //c-style string to save each number size_t j = 0; //strlen(temp) == j for(i=0; i<(wcslen(input) + 1);i++) { if(temp == NULL) if((temp = malloc(sizeof(char))) == NULL) exit(EXIT_FAILURE); //the delimiter is an unicode non-break-space ' ' if(input[i] == L' ' || i == wcslen(input)) { temp[j] = L'\0'; //c-style string, remember? buffer[count] = _wtoi(temp); //convert temp to int count++; if((buffer = realloc(buffer, (count + 1) * sizeof(int))) == NULL) exit(EXIT_FAILURE); j = 0; free(temp); temp = NULL; continue; } temp[j] = input[i]; j++; if((temp = realloc(temp, (j + 1) * sizeof(wchar_t))) == NULL) exit(EXIT_FAILURE); } for(i=0; i<count; i++) { //print out the result: printf("%d\n", buffer[i]); } free(buffer); //clean-up //happy ending...
If you use wchar_t *temp, then your malloc should also use
wchar_t here:
Moreover, you are only ever allocating one byte, but storing potentially more in the string.Code:if((temp = malloc(sizeof(char))) == NULL) exit(EXIT_FAILURE);
And temp is never freed, so it's a memory leak too.
I would suggest using a fixed size string for this purpose - a number that is longer than (say) 15 digits will not be a valid integer [unless it has redundant leading zeros of course], so there's no need to have a string bigger than 16 chars. Saves all sort of problems with allocating memory - but make sure that you detect longer strings before trying to copy.
I would also, unless there is clear need to support a HUGE number of numbers, have buffer as a statically sized array - again, simplifying the code [and avoiding problems with what to do when allocation fails].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Sorry, the code was ANSI before. I forget that one. Thanks.If you use wchar_t *temp, then your malloc should also use
wchar_t here:
Moreover, you are only ever allocating one byte, but storing potentially more in the string.Code:if((temp = malloc(sizeof(char))) == NULL) exit(EXIT_FAILURE);
Please take a look at...And temp is never freed, so it's a memory leak too.
At the end of the loop the temp is freed.Code:if(input[i] == L' ' || i == wcslen(input)) { //... j = 0; free(temp); temp = NULL; //... }
I wonder, where is it?But temp is always one wchar_t, and it contains '\0'.
Last edited by tabstop; 06-20-2008 at 10:43 AM.
Ok, I admit I didn't see that. So if i != wcslen(input) and the string doesn't end with a space, it won't do that, right - and I'm pretty sure that i < wcslen(input) in your for-loop will prevent it from hitting that bit of code...
Doing a realloc for every char in the string is REALLY bad coding practice. realloc itself is quite time consuming, and it will copy the content of the string each time [well, ok, so a decent implementation of realloc will just grow in place some of the time, but you don't know that this is how it's implement, nor when and under what conditons]. I still think you should use a fixed array size. Saves a lot of hazzle, and it's more efficient too.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.