给定一个字符串中的一些文本行,每行之间用’“性格。打印最后N行。如果行数小于N,则打印所有行。
null
这个问题的解决方法已经在本文中讨论过 Set-1 只印了10行。在这篇文章中,我们将讨论打印最后N行的另一种方法。
算法:
- 把绳子分开“使用 斯特托克 .
- 将单个字符串存储在向量中。
- 打印vector中的最后N个字符串。
以下是上述方法的实施情况:
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 ''. char * token = strtok (str, "" ); // Save all strings in the vector. while (token) { v.push_back(token); token = strtok (NULL, "" ); } if (v.empty()) { cout << "ERROR: string doesn't contain '\n' character" ; 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 = "str1str2str3str4str5str6str7str8str9" "str10str11str12str13str14str15str16str17" "str18str19str20str21str22str23str24str25" ; 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( "" ); 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 = "str1str2str3str4str5str6str7" + "str8str9str10str11str12str13" + "str14str15str16str17str18str19" + "str20str21str22str23str24str25" ; 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( '' ) if len (v) = = 0 : print ( "ERROR: string doesn't " , "contain '\n' character" ) 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 = "str1str2str3str4str5str6" + "str7str8str9str10str11" + "str12str13str14str15str16" + "str17str18str19str20str21" + "str22str23str24str25" 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( '' ); 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 = "str1str2str3str4str5str6str7" + "str8str9str10str11str12str13" + "str14str15str16str17str18str19" + "str20str21str22str23str24str25" ; int n = 10; printLast(s1, n); } } // This code is contributed by Rajput-Ji |
输出:
str16 str17 str18 str19 str20 str21 str22 str23 str24 str25
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END