15 Blue Bridge Cup 4th question

Source: Internet
Author: User
Tags truncated

Test instructions: The Stringingrid function prints the specified string in a grid of a specified size. Requires the string to be centered in a horizontal, vertical, two direction. If the string is too long, it is truncated. If you can't just center it, you can do it slightly to the left or to a point. The following program implements this logic, please fill in the missing code in the underlined section.
Train of thought: be all kinds of output empty dizzy, but look at the solution on the original problem, a take should be some in and out.
The output expected to be int len1 = (Width-2-strlen (s))/2; int len2 = (Width-1-Len1-strlen (s)); printf ("%*s%s%*s", Len1, "", S, Len2, ""); should be int len1 = (Width-2-strlen (s))/2; int len2 = (Width-1-Len1-strlen (s)); printf ("%*s%s%*s", Len1, "", S, Len2, ""); The error is that when len1 = = 0 o'clock, the first one will output more than one space. %*s, the preceding argument is the number of bits that the string occupies, followed by the string to be output. When the length of the string is not enough, it is padded with spaces. But not when it's over. So the above error will occur.

Attach the right code:

1 /*2 The stringingrid function prints the specified string in a grid of a specified size. 3 requires the string to be centered in a horizontal, vertical, two direction. 4 if the string is too long, it is truncated. 5 If you can't just center it, you can do it slightly to the left or to a point. 6 7 The following program implements this logic, please fill in the missing code in the underlined section. 8  */9 Ten#include <stdio.h> One#include <string.h> A  - voidStringingrid (intWidthintHeightConst Char*s) - { the     intI, K; -     Charbuf[1]; -strcpy (buf, s);//A character array of length 1, copy?  -     if(strlen (s) >width-2) Buf[width-2] =0;//truncates the string. Why is it truncated when it is greater than the width minus 2?  +                                                  //It means that you have to leave at least one lattice on both sides of the center.  -printf"+"); +      for(i =0; I<width-2; i++) printf ("-"); Aprintf"+\n");//Border Width at  -      for(k =1; k< (Height-1) /2; k++) -     { -printf""); -          for(i =0; I<width-2; i++) printf (" "); -printf"\ n"); in     } -  toprintf"");//????  +  -     //printf ("%*s%s%*s", (Width-strlen (s)-2)/2, "", S, (Width-strlen (s)-1)/2, "");//Output String! The format is the parameter the     intLen1 = (Width-2-Strlen (s))/2; *     intLen2 = (Width-1-Len1-strlen (s)); $printf"%*s%s%*s", Len1,"", S, Len2,"");Panax Notoginseng  -printf"\ n"); the  +      for(k = (Height-1) /2+1; K1; k++) A     { theprintf""); +          for(i =0; I<width-2; i++) printf (" "); -printf"\ n"); $     } $  -printf"+"); -      for(i =0; I<width-2; i++) printf ("-"); theprintf"+\n"); - }Wuyi  the intMain () - { WuStringingrid ( -,6,"abcd1234"); -     return 0; About}
View Code

15 Blue Bridge Cup 4th question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.