1

Hi i'm trying to write a recursive function to extract each line of a string. I don't know why it doesn't work i spent 2 hours trying to find the issue.

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

int safe_line(char* input, int end_of_line, int num_of_line, int num_sum_safe)
{

    //takes an input, the coordinates of the previous end of the line
    //the number of line already treated
    //and a num_sum_safe which is not important
    
    char temp_line[24];
    int count;
    int actual_line;
    int temp_num_line;
    actual_line = end_of_line;
    temp_num_line = num_of_line + 1;

    if (num_of_line == 3)
    {
        return num_sum_safe;
    }
    
    count = actual_line;

    while (*(input + count) != '\n')
    {   
        *(temp_line + count - actual_line) = *(input + count);
        count++;
    }
    *(temp_line + count - actual_line ) = '\0';
    actual_line += count + 1;

    printf("%s\n", temp_line);

    return safe_line(input, actual_line, temp_num_line, 0);
}

int main()
{
    char input[15] = "n\natt\naa\na as";
    safe_line(input,0 ,0 ,0 );
}

The value that it returns in stdout :

n

att

The expected value should be this :

n

att

naa

na as

7
  • Can you give any explanation as to why this output is expected? And what the input file that is expected to produce this output is? Commented Dec 2, 2024 at 18:41
  • 1
    First of all create a proper minimal reproducible example. Not only to show us but also for you to debug. The input should be hard-coded into the main function, and you should avoid as much dynamic allocation or similar operations that aren't strictly needed for debugging or testing. Then use a debugger to step through the code line by line, while monitoring variables and their values, to see what really happens. Commented Dec 2, 2024 at 18:50
  • Also please note that for any pointer p and index i, the expression *(p + i) is exactly equal to p[i]. The latter, p[i], is usually easier to read and understand. And also less to write. Commented Dec 2, 2024 at 18:51
  • And you copy-paste the expected output into your question as text. Why don't you copy-paste the actual output as text as well? Why do you post a link to an image? Commented Dec 2, 2024 at 18:52
  • 1
    Splitting a string is a rather linear thing. Why would you want to use a recursive function? At the very least you should not pass the start of the string each time, skipping the first n lines. It's like mowing your lawn with a hammer and with your hands tied at your back. Commented Dec 3, 2024 at 8:17

2 Answers 2

1

It seems the problem lies in this statement

actual_line += count + 1;
            ^^

You need to write just

actual_line = count + 1;
           ^^^ 

Nevertheless the function declaration is too complicated. Instead of always passing the initial address of the buffer and the current position inside it you could only pass a pointer inside the buffer using the pointer arithmetic.

Here is a demonstration program that shows how you can decrease the number of function parameters by means of the pointer arithmetic.

#include <stdio.h>

void f( const char *input )
{
    int i = 0;

    if (input[i] != 0)
    {
        while (input[i] != '\0' && input[i] != '\n') ++i;

        printf( "%.*s\n", i, input );

        if (input[i] != '\0')
        {
            f( input + i + 1 );
        }
    }
}

int main( void )
{
    char input[] = "n\natt\naa\na as";

    f( input );
}

The program output is

n
att
aa
a as
Sign up to request clarification or add additional context in comments.

1 Comment

YOU'RE THE GOAT
0

While an explanation of your mistake has already been made by Vlad from Moscow, it may be worth noting that this is a lot of effort to avoid using strtok.

E.g.

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

int main(void) {
    char line[] =
        "hello world\n"
        "foo bar\n"
        "baz wooble\n";

    char *token = strtok(line, "\n");
    while (token) {
       printf("> %s\n", token);
       token = strtok(NULL, "\n");
    }
}

Running this:

$ ./a.out
> hello world
> foo bar
> baz wooble

Comments

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.