Tuesday, July 3, 2007

Interesting C Program -3

Can you write a C program to print the String in the center of the Screen...
Heres a little demonstration of that.

/*
This program will move the string to the center of the screen.
Center in this context is a relative thing. So I have taken my screen size as 80
and did the program. The user has to change according to his size of the screen.
*/
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
void mystring(char* s);
int main()
{
char name[10];
mystring("ENTER YOUR NAME\n");
mystring("");
scanf("%s",&name);
mystring(name);
mystring("\n");
system("pause");
}
void mystring(char* s)
{
int screen_size = 80;
int l=strlen(s);
int pos=(int)((screen_size-l)/2);
for(int i=0;i < pos;i++ )
printf(" ");
printf("%s",s);
}

No comments:

Search Google

Books that I refer to...

  • The Complete Reference C, Fourth Edition
  • The C Programming Language