Program to print last 10 lines

16 Apr, 2019
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 steps1) 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 position3) Print the sub-string from target position.
C++
C
// 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 Codeint 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
/* 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 functionint 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;}
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.
rathbhupendra
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Multidimensional Arrays in C / C++
Function Pointer in C
Left Shift and Right Shift Operators in C/C++
Different Methods to Reverse a String in C++
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
Set in C++ Standard Template Library (STL)
Priority Queue in C++ Standard Template Library (STL)