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++
// 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; } |
Java
// Java program to print the last N lines import java.util.*; Ā
class GFG { Ā Ā Ā Ā static void printLast(String s, int t) Ā Ā Ā Ā { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Vector to store individual strings. Ā Ā Ā Ā Ā Ā Ā Ā // Save all strings in the vector. Ā Ā Ā Ā Ā Ā Ā Ā String[] v = s.split( "\n" ); Ā
Ā Ā Ā Ā Ā Ā Ā Ā if (v.length == 0 ) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println( "ERROR: string doesn't " + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "contain '\\n' character" ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return ; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the string has t lines Ā Ā Ā Ā Ā Ā Ā Ā if (v.length >= t) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = v.length - t; i < v.length; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(v[i]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0 ; i < v.length; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(v[i]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Driver Code Ā Ā Ā Ā public static void main(String[] args) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā 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); Ā Ā Ā Ā } } Ā
// This code is contributed by // sanjeev2552 |
Python3
# 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 |
C#
// C# program to print the last N lines using System; Ā
class GFG { Ā Ā Ā Ā static void printLast(String s, int t) Ā Ā Ā Ā { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // List to store individual strings. Ā Ā Ā Ā Ā Ā Ā Ā // Save all strings in the vector. Ā Ā Ā Ā Ā Ā Ā Ā String[] v = s.Split( '\n' ); Ā
Ā Ā Ā Ā Ā Ā Ā Ā if (v.Length == 0) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine( "ERROR: string doesn't " + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "contain '\\n' character" ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return ; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the string has t lines Ā Ā Ā Ā Ā Ā Ā Ā if (v.Length >= t) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = v.Length - t; i < v.Length; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(v[i]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0; i < v.Length; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(v[i]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Driver Code Ā Ā Ā Ā public static void Main(String[] args) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā 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); Ā Ā Ā Ā } } Ā
// This code is contributed by Rajput-Ji |
Javascript
function PrintLast( s, t) { Ā Ā Ā Ā Ā Ā Ā Ā Ā const v = s.split( "\n" ); Ā Ā Ā Ā if (v.length==0) { Ā Ā Ā Ā Ā Ā Ā Ā console.log( "ERROR: string doesn't contain '\\n' character\n" ); Ā Ā Ā Ā Ā Ā Ā Ā return ; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // If the string has t lines Ā Ā Ā Ā if (v.length >= t) { Ā Ā Ā Ā Ā Ā Ā Ā for (let i = v.length - t; i < v.length; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā console.log(v[i]); Ā Ā Ā Ā } Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā for (let i = 0; i < v.length; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā console.log(v[i]); Ā Ā Ā Ā } } Ā
Ā Ā Ā Ā let 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" ; Ā Ā Ā Ā let n = 10; Ā Ā Ā Ā PrintLast(s1, n); Ā
Ā Ā Ā Ā // This code is contributed by garg28harsh. |
str16 str17 str18 str19 str20 str21 str22 str23 str24 str25
Time Complexity: O(n)
Auxiliary Space: O(n)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!