Program to print last N lines | Set-2

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

An approach for this problem has been already discussed in Set-1 where only 10 lines were printed. In this post, another approach is discussed for printing the last N lines.

Algorithm:



  • Split the string around ‘\n’ using strtok.
  • Store the individual strings in a vector.
  • Print the last N strings from vector.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print the last N lines
#include <bits/stdc++.h>
using namespace std;
  
void PrintLast(string s, int t)
{
    // Vector to store individual strings.
    vector<string> v;
  
    // Get a pointer to string.
    char* str = &s[0];
    // Split the string around '\n'.
    char* token = strtok(str, "\n");
  
    // Save all strings in the vector.
    while (token) {
        v.push_back(token);
        token = strtok(NULL, "\n");
    }
  
    if (v.empty()) {
        cout << "ERROR: string doesn't contain '\\n' character\n";
        return;
    }
  
    // If the string has t lines
    if (v.size() >= t) {
        for (int i = v.size() - t; i < v.size(); i++)
            cout << v[i] << endl;
    }
    else {
        for (int i = 0; i < v.size(); i++)
            cout << v[i] << endl;
    }
}
  
// Driver Code
int main()
{
    string s1 = "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";
    int n = 10;
    PrintLast(s1, n);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print the last N lines 
def PrintLast(s, t): 
  
    # Vector to store individual strings. 
    v = s.split('\n')
      
    if len(v) == 0
        print("ERROR: string doesn't ",
              "contain '\\n' character\n"
        return
      
    # If the string has t lines 
    elif len(v) >= t: 
        for i in range(len(v) - t, len(v)): 
            print(v[i]) 
      
    else:
        for i in range(0, len(v)): 
            print(v[i])
      
# Driver Code 
if __name__ == "__main__":
  
    s1 = "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"
      
    n = 10
    PrintLast(s1, n) 
  
# This code is contributed by Rituraj Jain

chevron_right


Output:

str16
str17
str18
str19
str20
str21
str22
str23
str24
str25


My Personal Notes arrow_drop_up

A Coding Enthusiast Rails Developer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : rituraj_jain



Article Tags :
Practice Tags :


Be the First to upvote.


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