Source Code File 05-04_ctitle

05-04_ctitle.c

#include <ncurses.h>
#include <string.h>

void center(int row, char *title)
{
	int len,indent,y,width;

	/* get screen width */
	getmaxyx(stdscr,y,width);
	/* get title length */
	len = strlen(title);
	/* calculate indent */
	indent = (width - len)/2;
	/* show the string */
	mvaddstr(row,indent,title);
	refresh();
}

int main()
{
	initscr();

	center(1, "Penguin Soccer Finals");
	center(5, "Cattle Dung Samples from Temecula");
	center(7, "Catatonic Theater");
	center(9, "Why Do Ions Hate Each Other?");
	getch();

	endwin();
	return 0;
}

Output Screenshot

Notes

* Don't know why I skipped a row 3 title.

* A warning may appear after compiling, showing that variable y is unused. There's no way around this warning, as the variable is required by the getmaxyx() function. It doesn't present a problem.