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++
#include <bits/stdc++.h>
using namespace std;
#define DELIM '\n'
void print_last_lines( char *str, int n)
{
if (n <= 0)
return ;
size_t cnt = 0;
char *target_pos = NULL;
target_pos = strrchr (str, DELIM);
if (target_pos == NULL)
{
cout << "ERROR: string doesn't contain '\\n' character\n" ;
return ;
}
while (cnt < n)
{
while (str < target_pos && *target_pos != DELIM)
--target_pos;
if (*target_pos == DELIM)
--target_pos, ++cnt;
else
break ;
}
if (str < target_pos)
target_pos += 2;
cout << target_pos << endl;
}
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;
}
|
C
#include <stdio.h>
#include <string.h>
#define DELIM '\n'
void print_last_lines( char *str, int n)
{
if (n <= 0)
return ;
size_t cnt = 0;
char *target_pos = NULL;
target_pos = strrchr (str, DELIM);
if (target_pos == NULL)
{
fprintf (stderr, "ERROR: string doesn't contain '\\n' character\n" );
return ;
}
while (cnt < n)
{
while (str < target_pos && *target_pos != DELIM)
--target_pos;
if (*target_pos == DELIM)
--target_pos, ++cnt;
else
break ;
}
if (str < target_pos)
target_pos += 2;
printf ( "%s\n" , target_pos);
}
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;
}
|
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
-----------------
Time Complexity: O(n)
Auxiliary Space: O(1)
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Jul, 2022
Like Article
Save Article