Advertisement
SergeyPGUTI

8.1.9

Feb 14th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string.h>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. void center(char a[], int n)
  9. {
  10.     int length = strlen(a);
  11.     if (length>n) return ;
  12.     int spaceNumber=n-length;
  13.     int spaceNumberLeft=spaceNumber/2;
  14.     for (int i=length-1;i>=0;i--)// освобождаем место для пробелов - сместим строку на spaceNumberLeft пробелов
  15.     {
  16.         a[i+spaceNumberLeft]=a[i];
  17.     }
  18.     for (int i=0;i<spaceNumberLeft;i++)//рисуем пробелы слева
  19.     {
  20.         a[i]=' ';
  21.     }
  22.     for (int i=length+spaceNumberLeft;i<n;i++)// добавим пробелы справа
  23.     {
  24.         a[i]=' ';
  25.     }
  26.     a[n]=0;// конец строки
  27. }
  28.  
  29. int main()
  30. {
  31.     char str[30]="Hello";
  32.     center(str,10);
  33.     cout<<strlen(str);
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement