给定一个字符串的某些文本行,每行之间用’\ n’字符分隔。打印最后十行。如果行数少于10,则打印所有行。
资料来源:Microsoft专访|套装10
以下是步骤
1)查找最后出现的DELIM或’\ n’
2)将目标位置初始化为最后一次出现的’\ n’并计数为0,然后在count <10时执行以下操作
…… 2.a)找到“ \ n”的下一个实例并更新目标位置
….. 2.b)跳过“ \ n”并增加计数“ \ n”并更新目标位置
3)从目标位置打印子字符串。
C++
// C++ Program to print the last 10 lines.
// If number of lines is less than 10,
// then print all lines.
#include
using namespace std;
#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)
{
cout << "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
cout << target_pos << endl;
}
// Driver Code
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;
}
// This is code is contributed by rathbhupendra
C
/* Program to print the last 10 lines. If number of lines is less
than 10, then print all lines. */
#include
#include
#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;
}
输出:
str16
str17
str18
str19
str20
str21
str22
str23
str24
str25
-----------------
str1
str2
str3
str4
str5
str6
str7
-----------------
-----------------
ERROR: string doesn't contain '\n' character
-----------------
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。