Reputation: 312
I'm trying to build a recursive function that gets a string (char array) which should contain numbers seperated by spaces or tabs, and the amount of numbers which should be inserted, and insert each number to an int array. I can't use any loop for this function, only recursion. The number of spaces and tabs between two numbers are unknown. (although the char array is a 1000 chars buffer) I'm using gets_s for the input string. If there is wrong char in the string or incorrect amount of inserted numbers the function will print an error and restart itself (again whitout any loops). +/- signs and leading zeros (001) are allowed.
So far I have a function which gets a char array which contains a valid number and returns its value as int (for example {'-','1','5','\0'}
will return -15
).
What is the best way to convert the string into the int array in order to use this array next in my program?
Thanks
Upvotes: 0
Views: 145
Reputation: 15642
In the comments I wrote:
This kind of "don't use the most appropriate tool to solve" problem should be a red flag to you; your resource, whatever it is, is teaching you to solve problems in an inappropriate way. Why, in the real world, would anyone not use loops in C here? Alternatively, if the author wants to teach functional programming, why would they use a non-functional language likc C? Get a better book!
Nonetheless, here I find myself offering an answer...
First off, they're technically both loops! I gather when you state that you can't use any loop, you're actually referring to procedural loops (i.e. loops built from goto
, while
, do...while
or for
), and not functional loops (which are built by recursive function application). They're both technically loops, and since C is a procedural language you should use procedural loops. Nonetheless...
There's a pattern! Procedural loops can be converted to functional loops simply by observing and reproducing a pattern. That is, every procedural loop has an equivalent functional loop, and they all end up looking very similar. Look for the pattern below.
Here's a function containing a basic procedural loop:
void procedural_loop_on_str(char const *source, size_t source_position, int *dest, size_t dest_position) {
while (source[source_position] != '\0') {
source_position++;
}
}
... and here it is as a functional loop:
void functional_loop_on_str(char const *source, size_t source_position, int *dest, size_t dest_position) {
if (source[source_position] != '\0') {
source_position++;
functional_loop_on_str(source, source_position, dest, dest_position);
}
}
The two pieces of code seem very similar, don't they? Any procedural loop can be trivially translated to a functional equivalent based on this observation; all you'll be doing is adding the extra code that fills in your loop. As an exercise, consider isolating the parts of this code that are identical. They generally sit in the same place in both pieces of code; they're not hard to find! In fact, here are those differences, side-by-side:
while ... if ...
functional_loop_on_str...
The only difficulties come in place when backtracking is necessary, which is irrelevant for this question. Hence, you should be able to write the procedural version and translate it to a functional version easily. Allow me to start you off:
void procedural_loop_on_str(char const *source, size_t source_position, int *dest, size_t dest_position) {
while (source[source_position] != '\0') {
int value, nbytes, success = sscanf(source + source_position, "%d%n", &value, &nbytes);
if (!success) { nbytes = 1; }
else { /* XXX: You do this part!
* You need to do something with value, dest and dest_position */
}
source_position += nbytes;
}
}
... and here it is translated to our functional version by reproducing the pattern:
void functional_loop_on_str(char const *source, size_t source_position, int *dest, size_t dest_position) {
if (source[source_position] != '\0') {
int value, nbytes, success = sscanf(source + source_position, "%d%n", &value, &nbytes);
if (!success) { nbytes = 1; }
else { /* XXX: You do this part!
* You need to do something with value, dest and dest_position */
}
source_position += nbytes;
functional_loop_on_str(source, source_position, dest, dest_position)
}
}
Notice how little has actually changed between the two examples; most of the pattern is still there! Recall the exercise above? Well, you can do the same here to find that the same changes are made in the same place when converting from procedural to functional.
I think this pattern would've been obvious to you had you read a (better) book about functional programming and a (better) book about C programming, or even created these basic examples yourself to observe the pattern. Nonetheless, I stand by my beliefs that:
Either way, it appears you need a better book!
Upvotes: 1
Reputation: 580
sscanf sounds like the tool you need. It's the one I would use. You can recursively read your string into your int array. I would also create an int array with 501 positions, since that is the highest amount of ints you can get(if each int is just a single digit number and there is just one space between them).
Alternatively you could use atoi
Upvotes: 0
Reputation: 69
pass a pointer to the end of you integer array as an argument to the function s well as a pointer to the current character in the string. if not white space, add the number to the int array and increment the int pointer. increment the char pointer appropriatley regaurdless and recurse. I assume you can handle converting strings to ints, there are many posts to help with this if not.
Upvotes: 0