Line extraction in a char

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#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 ) = '';
actual_line += count + 1;
printf("%sn", temp_line);
return safe_line(input, actual_line, temp_num_line, 0);
}
int main()
{
char input[15] = "nnattnaana as";
safe_line(input,0 ,0 ,0 );
}
</code>
<code>#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 ) = ''; actual_line += count + 1; printf("%sn", temp_line); return safe_line(input, actual_line, temp_num_line, 0); } int main() { char input[15] = "nnattnaana as"; safe_line(input,0 ,0 ,0 ); } </code>
#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 ) = '';
    actual_line += count + 1;

    printf("%sn", temp_line);

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

int main()
{
    char input[15] = "nnattnaana 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

It seems the problem lies in this statement

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>actual_line += count + 1;
^^
</code>
<code>actual_line += count + 1; ^^ </code>
actual_line += count + 1;
            ^^

You need to write just

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>actual_line = count + 1;
^^^
</code>
<code>actual_line = count + 1; ^^^ </code>
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <stdio.h>
void f( const char *input )
{
int i = 0;
if (input[i] != 0)
{
while (input[i] != '' && input[i] != 'n') ++i;
printf( "%.*sn", i, input );
if (input[i] != '')
{
f( input + i + 1 );
}
}
}
int main( void )
{
char input[] = "nnattnaana as";
f( input );
}
</code>
<code>#include <stdio.h> void f( const char *input ) { int i = 0; if (input[i] != 0) { while (input[i] != '' && input[i] != 'n') ++i; printf( "%.*sn", i, input ); if (input[i] != '') { f( input + i + 1 ); } } } int main( void ) { char input[] = "nnattnaana as"; f( input ); } </code>
#include <stdio.h>

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

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

        printf( "%.*sn", i, input );

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

int main( void )
{
    char input[] = "nnattnaana as";

    f( input );
}

The program output is

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>n
att
aa
a as
</code>
<code>n att aa a as </code>
n
att
aa
a as

1

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <string.h>
#include <stdio.h>
int main(void) {
char line[] =
"hello worldn"
"foo barn"
"baz wooblen";
char *token = strtok(line, "n");
while (token) {
printf("> %sn", token);
token = strtok(NULL, "n");
}
}
</code>
<code>#include <string.h> #include <stdio.h> int main(void) { char line[] = "hello worldn" "foo barn" "baz wooblen"; char *token = strtok(line, "n"); while (token) { printf("> %sn", token); token = strtok(NULL, "n"); } } </code>
#include <string.h>
#include <stdio.h>

int main(void) {
    char line[] =
        "hello worldn"
        "foo barn"
        "baz wooblen";

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

Running this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ ./a.out
> hello world
> foo bar
> baz wooble
</code>
<code>$ ./a.out > hello world > foo bar > baz wooble </code>
$ ./a.out
> hello world
> foo bar
> baz wooble

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật