Program to print last 10 lines

本文介绍了一种从给定字符串中打印最后10行的方法,如果行数少于10,则打印所有行。该方法适用于面试准备和技术实现场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://www.geeksforgeeks.org/print-last-10-lines-of-a-given-file/


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.

/* 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 function
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
-----------------

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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值