Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <string.h>
- using namespace std;
- void center(char a[], int n)
- {
- int length = strlen(a);
- if (length>n) return ;
- int spaceNumber=n-length;
- int spaceNumberLeft=spaceNumber/2;
- for (int i=length-1;i>=0;i--)// освобождаем место для пробелов - сместим строку на spaceNumberLeft пробелов
- {
- a[i+spaceNumberLeft]=a[i];
- }
- for (int i=0;i<spaceNumberLeft;i++)//рисуем пробелы слева
- {
- a[i]=' ';
- }
- for (int i=length+spaceNumberLeft;i<n;i++)// добавим пробелы справа
- {
- a[i]=' ';
- }
- a[n]=0;// конец строки
- }
- int main()
- {
- char str[30]="Hello";
- center(str,10);
- cout<<strlen(str);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement