Program to print last 10 lines

Given some text lines in one string, each line is separated by ‘\n’ character. Print the last ten lines. If number of lines is less than 10, then print all lines.

Source: Microsoft Interview | Set 10

Following are the steps
1) Find the last occurrence of DELIM or ‘\n’
2) Initialize target position as last occurrence of ‘\n’ and count as 0 , and do following while count < 10
……2.a) Find the next instance of ‘\n’ and update target position
…..2.b) Skip ‘\n’ and increment count of ‘\n’ and update target position
3) Print the sub-string from target position.

C++



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to print the last 10 lines. 
// If number of lines is less than 10,
// then print all lines. 
#include <bits/stdc++.h>
using namespace std; 
#define DELIM '\n' 
  
/* Function to print last n lines of a given string */
void print_last_lines(char *str, int n) 
    /* Base case */
    if (n <= 0) 
    return
  
    size_t cnt = 0; // To store count of '\n' or DELIM 
    char *target_pos = NULL; // To store the output position in str 
  
    /* Step 1: Find the last occurrence of DELIM or '\n' */
    target_pos = strrchr(str, DELIM); 
  
    /* Error if '\n' is not present at all */
    if (target_pos == NULL) 
    
        cout << "ERROR: string doesn't contain '\\n' character\n"
        return
    
  
    /* Step 2: Find the target position from
       where we need to print the string */
    while (cnt < n) 
    
        // Step 2.a: Find the next instance of '\n' 
        while (str < target_pos && *target_pos != DELIM) 
            --target_pos; 
  
        /* Step 2.b: skip '\n' and increment count of '\n' */
        if (*target_pos == DELIM) 
            --target_pos, ++cnt; 
  
        /* str < target_pos means str has 
        less than 10 '\n' characters, so break from loop */
        else
            break
    
  
    /* In while loop, target_pos is decremented 2 times, 
    that's why target_pos + 2 */
    if (str < target_pos) 
        target_pos += 2; 
  
    // Step 3: Print the string from target_pos 
    cout << target_pos << endl; 
  
// Driver Code
int main(void
    char *str1 ="str1\nstr2\nstr3\nstr4\nstr5\nstr6\nstr7\nstr8\nstr9"
                "\nstr10\nstr11\nstr12\nstr13\nstr14\nstr15\nstr16\nstr17"
                "\nstr18\nstr19\nstr20\nstr21\nstr22\nstr23\nstr24\nstr25"
    char *str2 ="str1\nstr2\nstr3\nstr4\nstr5\nstr6\nstr7"
    char *str3 ="\n"
    char *str4 = ""
  
    print_last_lines(str1, 10); 
    cout << "-----------------\n"
  
    print_last_lines(str2, 10); 
    cout << "-----------------\n";
  
    print_last_lines(str3, 10); 
    cout << "-----------------\n";; 
  
    print_last_lines(str4, 10); 
    cout << "-----------------\n";
  
    return 0; 
  
// This is code is contributed by rathbhupendra

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Program to print the last 10 lines. If number of lines is less
   than 10, then print all lines. */
  
#include <stdio.h>
#include <string.h>
#define DELIM   '\n'
  
/* Function to print last n lines of a given string */
void print_last_lines(char *str, int n)
{
    /* Base case */
    if (n <= 0)
       return;
  
    size_t cnt  = 0; // To store count of '\n' or DELIM
    char *target_pos   = NULL; // To store the output position in str
  
    /* Step 1: Find the last occurrence of DELIM or '\n' */
    target_pos = strrchr(str, DELIM);
  
    /* Error if '\n' is not present at all */
    if (target_pos == NULL)
    {
        fprintf(stderr, "ERROR: string doesn't contain '\\n' character\n");
        return;
    }
  
    /* Step 2: Find the target position from where we need to print the string */
    while (cnt < n)
    {
        // Step 2.a: Find the next instance of '\n'
        while (str < target_pos && *target_pos != DELIM)
            --target_pos;
  
         /* Step 2.b: skip '\n' and increment count of '\n' */
        if (*target_pos ==  DELIM)
            --target_pos, ++cnt;
  
        /* str < target_pos means str has less than 10 '\n' characters,
           so break from loop */
        else
            break;
    }
  
    /* In while loop, target_pos is decremented 2 times, that's why target_pos + 2 */
    if (str < target_pos)
        target_pos += 2;
  
    // Step 3: Print the string from target_pos
    printf("%s\n", target_pos);
}
  
// Driver program to test above function
int main(void)
{
    char *str1 = "str1\nstr2\nstr3\nstr4\nstr5\nstr6\nstr7\nstr8\nstr9"
                 "\nstr10\nstr11\nstr12\nstr13\nstr14\nstr15\nstr16\nstr17"
                 "\nstr18\nstr19\nstr20\nstr21\nstr22\nstr23\nstr24\nstr25";
    char *str2 = "str1\nstr2\nstr3\nstr4\nstr5\nstr6\nstr7";
    char *str3 = "\n";
    char *str4 = "";
  
    print_last_lines(str1, 10);
    printf("-----------------\n");
  
    print_last_lines(str2, 10);
    printf("-----------------\n");
  
    print_last_lines(str3, 10);
    printf("-----------------\n");
  
    print_last_lines(str4, 10);
    printf("-----------------\n");
  
    return 0;
}

chevron_right


Output:

str16
str17
str18
str19
str20
str21
str22
str23
str24
str25
-----------------
str1
str2
str3
str4
str5
str6
str7
-----------------

-----------------
ERROR: string doesn't contain '\n' character
-----------------

Note: Above program can be modified to print last N lines by passing N instead of 10. N can store any integer value.
This article is compiled by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : rathbhupendra



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.